使用cron计划读取文本文件的每一行

使用cron计划读取文本文件的每一行

本文介绍了使用cron计划读取文本文件的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用cron日程来读取文本文件的每一行。

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

但是GAE只允许读取文本文件而不修改它。

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

如何使请求知道上一行已经读取,它应该读取下一行。我不想存储所有的读取行在数据存储,然后做查询,检查文本行是否在数据存储区中,bla bla,因为我有百万行。

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.

请咨询。我在想Google Drive Api,有什么想法吗?感谢

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

更新:我可以这样做

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


推荐答案

Cron不会让你保存状态,除了在数据库中。因为你不想使用DB,你可以使用延迟任务代替,启动它一次,它将一直持续到最后一行被读取,如:

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
    deferred.defer( read_deferred, _countdown = 60 ) # 60 sec = 1 min

这篇关于使用cron计划读取文本文件的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:58