我正在运行一个启动多个任务的脚本:
for i in range (0,5):
taskqueue.add(url='/example',
params={'num': i})
据我了解,这些任务是并行运行的。一旦我刚刚插入队列的所有任务全部完成,我是否可以告诉 AppEngine 运行特定的任务/python 文件?我考虑过向在上一次循环迭代中调用的任务发送一个标志,但如果任务并行运行,则不能保证一旦完成,其他任务也将完成。
谢谢,
乔尔
最佳答案
当您启动任务时,您知道会有多少任务。将具有“预期”计数或任务列表的实体插入数据存储区。然后使用计数器或标记来指示任务何时运行。粗略地说,该过程可能类似于:
新种类:
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.
具体过程会根据您的用例的具体情况略有不同,例如您插入的任务数量。
关于google-app-engine - 在任务队列为空后调用脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4224564/