2014-10-09 167 views
1

Task dependency芹菜中的任務依賴關係

這樣的任務依賴關係可能嗎? 1和2可以並行執行。 1a只能在1完成時執行,但如果1和2都完成,則可以執行12b。我知道我可以讓1和2組成一組,然後組(1,2)| 12b可以是一個鏈條,但是如何使它成爲1a在1完成後開始,無論2是怎麼回事?

回答

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.