当 celery 中的任务组完成时发送成功信号

Send a success signal when the group of tasks in celery is finished

所以我有一个基本配置django 1.6 + celery 3.1。假设我有一个示例任务:

@app.task
def add(x, y):
    time.sleep(6)
    return {'result':x + y}

以及一个分组和 returns 作业 ID

的函数
def nested_add(x,y):
   grouped_task = group(add.s(x,y) for i in range(0,2))
   job = result_array.apply_async()
   job.save()
   return job.id

现在我想在这组任务完成时执行一些操作,但是如果我将 app.task 装饰器 放到 nested_add 并尝试捕获 task_success 然后它不会正常工作。我应该使用什么提示?

实际上有几种选择。最简单的是使用和弦。 Chord 会一直哀号,直到所有子任务都完成了一些结果,然后 return 返回总体结果。可以找到更多 http://ask.github.io/celery/userguide/tasksets.html. Another simple approach is to leverage AsyncResult API collect() method. More could be found here: http://celery.readthedocs.org/en/latest/reference/celery.result.html.

不要忘记配置结果后端。可以找到更多 http://celery.readthedocs.org/en/latest/getting-started/first-steps-with-celery.html#keeping-results。如果您使用 RabbitMQ 作为代理,那么也将其配置为结果后端。