问题
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