11

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?

Nikita Hismatov
  • 1,546
  • 1
  • 13
  • 30
  • 1
    What about raising an exception in the task when you want it to stop? – RickyA Oct 10 '12 at 13:12
  • Raising an exception didn't work for me. I have posted a detailed explanation here: http://stackoverflow.com/questions/17461374/celery-stop-execution-of-a-chain – Salami Jul 04 '13 at 03:32

1 Answers1

5

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)
Andrew Walker
  • 40,984
  • 8
  • 62
  • 84