芹菜中的任务依赖

时间:2014-10-09 15:49:47

标签: python celery django-celery

Task dependency

这样的任务依赖性是否可能? 1和2可以并行执行。 1a只能在1完成时执行,但如果1和2都完成则可以执行12b。

我知道我可以将1和2组成一组,然后组(1,2)| 12b可以是一个链但是如何制作它以便1a在1完成后开始,无论2发生了什么?

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。这是一种方法。我使用芹菜信号task_success连接到触发celery task

的函数

<强> my_tasks.py

from celery import Celery, task
from celery.signals import task_success

c = Celery('my_tasks')

@task
def t1():
    print('t1')

@task
def t2():
    print('t2')

@task
def t11():
    print('t11')

@task
def t12():
    print('t12')

def trigger_task(*args, **kwargs):
    t11.s().delay()

task_success.connect(trigger_task, sender=t1)

测试任务:

In [6]: complex_task = chain(group(t1.s(), t2.s())(), t12.si().delay())

这是日志。

[2014-10-10 12:31:05,082: INFO/MainProcess] Received task: my_tasks.t1[25dc70d2-263b-4e70-b9f2-56478bfedab5]
[2014-10-10 12:31:05,083: INFO/MainProcess] Received task: my_tasks.t2[0b0c5eb6-78fa-4900-a605-5bfd55c0d309]
[2014-10-10 12:31:05,084: INFO/MainProcess] Received task: my_tasks.t12[b08c616d-7a2d-4f7b-9298-2c8324b747ff]
[2014-10-10 12:31:05,084: WARNING/Worker-1] t1
[2014-10-10 12:31:05,084: WARNING/Worker-4] t2
[2014-10-10 12:31:05,085: WARNING/Worker-3] t12
[2014-10-10 12:31:05,086: INFO/MainProcess] Task my_tasks.t2[0b0c5eb6-78fa-4900-a605-5bfd55c0d309] succeeded in 0.00143978099914s: None
[2014-10-10 12:31:05,086: INFO/MainProcess] Task my_tasks.t1[25dc70d2-263b-4e70-b9f2-56478bfedab5] succeeded in 0.00191083699974s: None
[2014-10-10 12:31:05,087: INFO/MainProcess] Task my_tasks.t12[b08c616d-7a2d-4f7b-9298-2c8324b747ff] succeeded in 0.00184817300033s: None
[2014-10-10 12:31:05,087: INFO/MainProcess] Received task: my_tasks.t11[a3e3f0c6-ac1f-4888-893a-02eee3b29585]
[2014-10-10 12:31:05,088: WARNING/Worker-2] t11
[2014-10-10 12:31:05,089: INFO/MainProcess] Task my_tasks.t11[a3e3f0c6-ac1f-4888-893a-02eee3b29585] succeeded in 0.000978848000159s: None

我尝试直接连接到任务,但it is throwing some error.