python - Celery 在其他任务完成时启动任务

标签 python django celery

我在 Celery 中有 3 个任务..

celery_app.send_task('tasks.read_cake_recipes')
celery_app.send_task('tasks.buy_ingredients')

celery_app.send_task('tasks.make_cake')

read_cake_recipesbuy_ingredients 没有任何依赖关系,但是在任务 make_cake 可以运行之前 read_cake_recipesbuy_ingredients 需要完成。

make_cake 可以在前两个开始后随时运行。但是 make_cake 不知道其他任务是否已经完成。因此,如果 read_cake_recipesbuy_ingredients 花费的时间太长,那么 make_cake 就会失败。

链接任务在这里似乎不起作用,因为 make_cake 有多个依赖项。

我怎样才能继续启动任务 make_cake,然后让它等待/挂起等等,直到其他两个任务首先完成?

我的可取之处是 read_cake_recipes 和 buy_ingredients 将结果保存到数据库中,如果 make_cake 不知何故知道要查找哪些成分或食谱,它可以检查一下,也许吧?

最佳答案

完全是在猜测你的底层架构,但这里是..

class Cake(models.Model):
    recipes_read = models.BooleanField(default=False)
    ingredients_purchased = models.BooleanField(default=False)
    batter_prepared = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        if self.recipes_read and self.ingredients_purchased:
            self.batter_prepared = True
        super(Cake, self).save(*args, **kwargs)


@task
read_cake_recipes():
    for cake in Cake.objects.all():
        # Read some shit!
        cake.recipes_read = True
        cake.save()

@task
buy_cake_ingredients():
    for cake in Cake.objects.all():
        # Buy some shit!
        cake.ingredients_purchased = True
        cake.save()

@task
make_cake():
    for cake in Cake.objects.filter(batter_prepared=True):
        # Make that shit!

关于python - Celery 在其他任务完成时启动任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25576890/

相关文章:

python - 如何将 spark 数据帧保存到 HDFS 上的 csv?

django - Python Social Auth为不同用户复制电子邮件

javascript - 在 headless 模式下针对 webkit 渲染引擎测试页面

python - 无法在docker容器中启动rabbitmq-server,这个Dockerfile怎么写?

python - Celery 正确的任务组链

Celery:如何从自定义状态检索元数据?

python - 使用 pandas get_loc 更有效的方法?

python - 如何监控 networkx 图创建的状态?

python - 如何在没有 DOTALL 的情况下匹配任何东西 (DOTALL)?

python - 如何从请求帖子中解析我的 View 中的 json