芹菜在其他任务完成时启动任务

时间:2014-08-29 22:06:58

标签: python django 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以某种方式知道要查找哪些成分或配方可以检查这个,可能吗?

1 个答案:

答案 0 :(得分:1)

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

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!