芹菜任务链取消?

时间:2012-10-07 20:22:55

标签: python celery delayed-execution

我发现芹菜支持任务链:http://celery.readthedocs.org/en/latest/userguide/canvas.html#chains

问题是:如何在任务中停止链的执行?

例如,我们获得了N个项目链(N> 2)。在第二个任务中,我们意识到我们不需要执行所有其余任务。怎么办?

1 个答案:

答案 0 :(得分:4)

在较新版本的芹菜(3.1.6)中,您可以通过简单地走链并依次撤销每个项目来撤销整个链。

# Build a chain for results
from tasks import addd
from celery import chain

def revoke_chain(result):
    while result:
        result.revoke()
        result = result.parent

# independent tasks (with immutable signatures)
c = chain(*tuple(add.si(i,i) for i in xrange(50)))
h = c()

# some time later ...
revoke_chain(h)

# dependant task
c = add.s(1,1) | add.s(2) | add.s(3)
h = c()

# some time later ...
revoke_chain(h)