我需要运行四个任务,但只有在成功的情况下,它们才需要一个接一个地运行。
我试图像这样将它们链接起来..但他们独立开始
res = (mul.si(5,5) | mul.si(5,6) | mul.si(5,7) | mul.si(5,8) | mul.si(5,9) )()
知道吗?
是的,你可以做到。但是您需要存储此处提到的结果。
但这很少使用,因为它将异步调用转换为同步调用
在我的示例中,
tasks.py 就像
from celery import Celery
import datetime
app = Celery('tasks',backend='amqp' broker='amqp://guest@localhost//')
@app.task
def add(x, y):
return x + y
def add_chained(args_list=list()):
for args in args_list:
print "Performing addtion for %s at %s" % (args, datetime.datetime.now())
result = add.delay(*args)
while not result.ready():
pass
结果是这样的:
>>> import tasks
>>> tasks.add_chained([(1,2), (2,3), (3,4), (4,5)])
Performing addtion for (1, 2) at 2014-01-17 18:49:57.392357
Performing addtion for (2, 3) at 2014-01-17 18:49:57.428961
Performing addtion for (3, 4) at 2014-01-17 18:49:57.432598
Performing addtion for (4, 5) at 2014-01-17 18:49:57.435891