可能的芹菜任务死锁?

时间:2014-10-11 17:06:11

标签: python celery deadlock

我遇到这种情况:

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名芹菜工人的理论情况下呢?

1 个答案:

答案 0 :(得分:1)

您正在等待group任务内other任务的结果。所以即使有一个芹菜工人也可能导致死锁。

让任务等待另一个任务的结果非常低效,如果工作池耗尽,甚至可能导致死锁。

注意:这只是在Celery 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)
相关问题