tom*_*low 5 python cron google-app-engine task-queue
我每天都有一个cron工作来调用API并获取一些数据.对于数据的每一行,我启动一个任务队列来处理数据(这涉及通过其他API查找数据).一旦完成所有这些,我的数据在接下来的24小时内不会改变,所以我记得它.
有没有办法知道我排队的所有任务何时完成,以便我可以缓存数据?
目前我通过安排两个这样的cron作业以一种非常混乱的方式做到这一点:
class fetchdata(webapp.RequestHandler):
def get(self):
todaykey = str(date.today())
memcache.delete(todaykey)
topsyurl = 'http://otter.topsy.com/search.json?q=site:open.spotify.com/album&window=d&perpage=20'
f = urllib.urlopen(topsyurl)
response = f.read()
f.close()
d = simplejson.loads(response)
albums = d['response']['list']
for album in albums:
taskqueue.add(url='/spotifyapi/', params={'url':album['url'], 'score':album['score']})
class flushcache(webapp.RequestHandler):
def get(self):
todaykey = str(date.today())
memcache.delete(todaykey)
Run Code Online (Sandbox Code Playgroud)
然后我的cron.yaml看起来像这样:
- description: gettopsy
url: /fetchdata/
schedule: every day 01:00
timezone: Europe/London
- description: flushcache
url: /flushcache/
schedule: every day 01:05
timezone: Europe/London
Run Code Online (Sandbox Code Playgroud)
基本上 - 我猜测我的所有任务都不会花费超过5分钟的时间来运行,所以我只需要在5分钟后刷新缓存,这样可以确保数据缓存时完成.
有没有更好的编码方式?感觉像我的解决方案不是最好的....
谢谢汤姆
目前还没有任何方法可以确定您的任务何时完成执行.您最好的选择是在数据存储区中插入标记记录,并让每个任务在完成后删除其记录.然后,每个任务都可以检查它是否是最后一个任务,如果是,则执行清理/缓存.
归档时间: |
|
查看次数: |
677 次 |
最近记录: |