Celery task chain cancelling?

笑着哭i 提交于 2019-12-12 08:35:14

问题


I found that celery supports task chains: http://celery.readthedocs.org/en/latest/userguide/canvas.html#chains.

Question is: how can I stop chain's execution in a task?

For example, we got a chain of N items (N > 2). And in the second task we realize that we do not need all the rest tasks to be executed. What to do?


回答1:


In newer versions of celery (3.1.6) you can revoke an entire chain by simply walking the chain and revoking each item in turn.

# 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)


来源:https://stackoverflow.com/questions/12772556/celery-task-chain-cancelling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!