2014-10-11 56 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)