Script call after task queue is empty

I run a script that initiates several tasks:

for i in range (0,5):
  taskqueue.add(url='/example', 
                          params={'num': i})

As I understand it, tasks are performed in parallel. Is there anyway I can tell AppEngine to run a specific task / python file, as soon as all the tasks that I just put in the queue, ALL finished? I thought about sending a flag to a task that was called in the last iteration of the loop, but if the tasks are executed in parallel, it does not check that the rest were completed after its completion.

Thank,

Joel

+5
source share
1 answer

, , . "" . , , , . , :

:

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.

usecase, , .

+4

All Articles