2014-10-11 55 views
2

我有這種情況:可能的芹菜任務死鎖?

tasks.py

@task 
def add(a,b): 
    return a+b 

@task 
def other(): 
    chunks = [1,1,1,1] # dummy data 

    for index in range(3): 
    # wait for each group to finish then continue to the next one 
    res = group(add.s(i,i) for i in chunks).apply_async() 
     # sleep for 1 second if group is not ready 
     while not res.get(): 
      time.sleep(1) 

難道這導致死鎖在等待組任務的完成?即使在只有一名芹菜工的理論情況下?

回答

1

您正在等待group任務結果other任務。所以即使有一名芹菜工人也可能導致死鎖。

讓一個任務等待另一個任務的結果真的是效率低下,甚至如果工作池耗盡,甚至可能導致死鎖。

注意:這只是在芹菜3.1中發出警告。但從Celery 3.2起,它將引發一個例外。

所以,最好讓你的設計是異步的。你可以做一個簡單的修改。

@task 
def other(): 

    chunks = [1, 1, 1, 1] 
    my_tasks = [] 

    for i in range(3): 
     # delay is shorthand for apply_async. 
     # using si to make signature immutable,so that its arguments don't change 
     group_task = group(add.si(i, i) for i in chunks).delay() 
     # here instead of executing them immediately, lets chain them 
     my_tasks.append(group_task) 

    from celery import chain 
    chain(my_tasks)