我每天都有一个cron作业来调用API并获取一些数据。对于每行数据,我启动一个任务队列来处理数据(包括通过其他API查找数据)。一旦所有这些都完成了,我的数据在接下来的24小时内不会改变,所以我memcache它。当appengine上的任务队列为空时运行函数
有没有办法知道我排队的所有任务何时完成,以便可以缓存数据?
目前我做一个真正的时尚凌乱通过只安排2个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)
然后我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
基本上 - 我猜测我所有的任务都不会超过5分钟,所以我只是在5分钟后刷新缓存,这确保了数据缓存完成。
有没有更好的编码方式?感觉就像我的解决方案是不是最好的....
感谢 汤姆
谢谢尼克 - 很高兴知道我没有错过任何明显的事情。我会尝试你的建议 – tomcritchlow 2011-04-05 03:36:32