我在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_recipes
和buy_ingredients
都没有任何依赖关系,但在运行任务make_cake
之前,read_cake_recipes
和buy_ingredients
都需要完成。
make_cake
可以在前两个启动后的任意时间运行。但是make_cake
不知道其他任务是否已经完成。因此,如果read_cake_recipes
或buy_ingredients
花费的时间太长,那么make_cake就会惨败。
链接任务在这里似乎不起作用,因为make_cake
有多个依赖项。
我如何仍然启动任务make_cake
,但让它等待/挂起等,直到其他两个任务首先完成?
我的可取之处在于,read_cake_recipes和buy_incredients将结果保存到DB中,如果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!