google-app-engine - 在任务队列为空后调用脚本

标签 google-app-engine

我正在运行一个启动多个任务的脚本:

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/

相关文章:

javascript - 我怎样才能使 go Time 与 js Date 兼容

python - Google App Engine 上的信用卡付款和通知

java - 按 ID 以外的属性获取实体

python - WTForms:我将每个字段单独绑定(bind)到对象上,有没有办法一次性将对象绑定(bind)到表单?

java - Android Studio appengine 端点不包括构建器

google-app-engine - 谷歌应用引擎中的嵌套命名空间

python - 日期时间和 utctimetuple()

java - Google Eclipse 插件 : Browser Plugin vs. 开发模式

php - Google App Engine - 使用 mysqli 通过 php 连接到 SQL Cloud 实例

java - 我一直收到 'No class was registered (through reference chain: ... )' ,但我注册了我的实体