检测 celery 任务和所有子任务何时完成
我有一个父任务,它将产生任意且可能大量的子任务。一旦父任务和所有子任务都完成,我需要在数据库中设置一个标志以指示它已准备就绪。我最好怎样做呢?
例如:
@task()
def master_task(foo):
foo_obj = Foo.objects.get(id=foo)
for bar in foo_obj.bar_set.all():
more_work.delay(bar.id)
@task()
def more_work(bar):
bar_obj = Bar.objects.get(id=bar)
do_work()
我需要检测 master_task 及其产生的所有子任务何时完成,以便我可以在相关模型上设置一个标志来指示一切都已准备就绪
I have a parent task that will spawn an arbitrary and potentially largish number of subtasks. Once both the parent and all of the subtasks have completed I need to set a flag in my database to indicate that it's ready. How would I best go about doing that?
For example:
@task()
def master_task(foo):
foo_obj = Foo.objects.get(id=foo)
for bar in foo_obj.bar_set.all():
more_work.delay(bar.id)
@task()
def more_work(bar):
bar_obj = Bar.objects.get(id=bar)
do_work()
I need to detect when the master_task and all of the subtasks it has spawns have completed so that I can set a flag on a related model to indicate that everything is ready
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用和弦
You should use a [TaskSet][1]:
> The TaskSet enables easy invocation of several tasks at once, and is then able to join the results in the same order as the tasks were invoked.
Use chords
You should use a [TaskSet][1]:
> The TaskSet enables easy invocation of several tasks at once, and is then able to join the results in the same order as the tasks were invoked.
celery.chord 正是为此而设计的。
celery.chord is designed precisely for that.