当您启动任务时,您知道会有多少个任务。将具有“预期”计数或任务列表的实体插入到数据存储中。然后使用计数器或标记来指示任务何时运行。粗略地说,该过程可能类似于:
新种类:
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)})
And the 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.
确切的过程会略有不同,具体取决于您的用例的具体情况,例如您插入的任务数量。