read each line of a text file using cron schedule

有些话、适合烂在心里 提交于 2019-12-12 00:43:17

问题


I want to use cron schedule to read each line of a text file.

cron:
- description: read a line of text file
  url: /read
  schedule: every 1 minutes

But GAE only allow to read a text file without modifying it.

How can I make the request read to know that the previous line was already read and it should read the next line. I dont want to store all the read line in datastore and then do query, check if the text line is in datastore or not, bla bla, because I have million of lines.

Please advice. I am thinking about Google Drive Api, any idea about that? Thanks

UPDATE: Can I do like this

with open('text.txt', 'r') as f:
        txt= f.readlines()
f.close()

t = txt[line_num]
DO SOMETHING
line_num = line_num+1
deferred.defer( read_deferred, line_num, _countdown = 60 ) # 60 sec = 1 min

and in somewhere else, I trigger deffered by calling

deferred.defer( read_deferred, 0, _countdown = 60 ) # 60 sec = 1 min

回答1:


Cron won't let you save the state, except in database. Since you don't want to use DB, you may use deferred task instead, start it once and it will keep going until the last line is read, something like:

def read_deferred(last_pos) :
    f = open( ... )

    if last_pos : f.seek(last_pos)

    # here you read the next line and do whatever you like,
    # just don't go further and return once you hit EOF

    last_pos = f.tell()

    deferred.defer( read_deferred, last_pos, _countdown = 60 ) # 60 sec = 1 min

and you call it first time like:

    deferred.defer( read_deferred, _countdown = 60 ) # 60 sec = 1 min


来源:https://stackoverflow.com/questions/15527617/read-each-line-of-a-text-file-using-cron-schedule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!