我正在运行一个启动多项任务的脚本:
for i in range (0,5):
taskqueue.add(url='/example',
params={'num': i})
据我了解,这些任务是并行运行的。无论如何,一旦我插入队列的所有任务全部完成,我可以告诉AppEngine运行特定的任务/ python文件吗?我考虑过向最后一次循环迭代中调用的任务发送一个标志,但如果任务并行运行,则不保证一旦完成,其他任务也完成了。
谢谢,
乔尔
答案 0 :(得分:4)
当您启动任务时,您知道将会有多少任务。使用“预期”计数或任务列表将实体插入数据存储区。然后使用计数器或标记来指示任务何时运行。粗略地说,这个过程看起来像是:
新种类:
class TaskBatch(db.Model):
expected_count = db.IntegerProperty()
class TaskMarker(db.Model):
pass
然后,调整您的调用例程以执行以下操作:
count = 5
taskbatch = TaskBatch(expected_count=count).put()
for i in range(5):
taskqueue.add(url='/example',
params={'num': i, 'batch': str(taskbatch)})
并且,在任务结束时:
def post(self):
num = self.request.get('num')
# do your stuff....
batch = self.request.get('batch')
TaskMarker(key_name=num, parent=db.Key(batch))
taskqueue.add(url='/example/isdone',
params={'num': i, 'batch': str(taskbatch)})
isdone
任务看起来像:
def post(self):
num = self.request.get('num')
batch_key = db.Key(self.request.get('batch'))
batch = TaskBatch.get(batch_key)
marker_keys = [db.Key.from_path('TaskMarker', i, parent=batch)
for i in range(batch.expected_count)]
markers = db.get(marker_keys)
if all(markers):
# do your done action.
具体过程会略有不同,具体取决于您的用例的具体情况,例如您插入的任务数量。