检测 celery 任务和所有子任务何时完成

发布于 2024-11-17 17:44:24 字数 403 浏览 5 评论 0原文

我有一个父任务,它将产生任意且可能大量的子任务。一旦父任务和所有子任务都完成,我需要在数据库中设置一个标志以指示它已准备就绪。我最好怎样做呢?

例如:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木落 2024-11-24 17:44:24

使用和弦


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.

朦胧时间 2024-11-24 17:44:24

celery.chord 正是为此而设计的。

celery.chord is designed precisely for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文