所以我有两个需要并行调用的任务,然后如果他们成功/失败我需要做一些事情,我还需要将他们的结果用于另一个任务。我会在这里试一试白板,试图让它更清晰:
| |
| |
add subtract
/ \ / \
/ \ / \
success \ / success
or fail \ / or fail
task \ / task
\ /
\ /
\ /
email results
我认为和弦可能是最好的选择,因为我可以并行执行加法和减法,然后获取结果并执行另一个回调。
以下是我的任务代码:
@shared_task
def add(x):
print 'adding'
return x + 5
@shared_task
def subtract(x):
print 'subtracting'
return x - 5
@shared_task(ignore_result=True)
def add_subtract_task_success(result):
if result == 10:
print 'task_success'
@shared_task
def add_subtract_task_failure(parent_task_id):
print 'link_error failure'
@shared_task
def email_results(dict):
print 'This is to be emailed: ' + str(dict)
所以在django shell中玩,我尝试了以下内容:
>>> callback = email_results.s()
>>> header = [add.s(5).apply_async(link=add_subtract_task_success.s(), link_error=add_subtract_task_failure.s()), subtract.s(5).apply_async(link=add_subtract_task_success.s(), link_error=add_subtract_task_failure.s())]
>>>
>>> result = chord(header)(callback)
这是它产生的例外:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/celery-3.1.17-py2.7.egg/celery/canvas.py", line 636, in __call__
return self.apply_async((), {'body': body} if body else {}, **options)
File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/celery-3.1.17-py2.7.egg/celery/canvas.py", line 631, in apply_async
parent = _chord(self.tasks, body, args, **options)
File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/celery-3.1.17-py2.7.egg/celery/app/task.py", line 420, in __call__
return self.run(*args, **kwargs)
File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/celery-3.1.17-py2.7.egg/celery/app/builtins.py", line 329, in run
maybe_signature(s, app=app).clone() for s in tasks
AttributeError: 'AsyncResult' object has no attribute 'clone'
欢迎任何想法和建议,非常感谢。