Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a Celery Task run only after the previous task is successful

Tags:

python

celery

I have two celery tasks, I want the second task to run only if the first is successful, something like this

@celery.task()
def add_together(a, b):
    return a + b

@celery.task()
def subtract(a, b):
    return a - b

First call

add_together.delay(2,2)

Second call

subtract.delay(4,2)

I want the second to run only if the first is successful

like image 822
zaddyn00b Avatar asked Oct 30 '25 16:10

zaddyn00b


1 Answers

Instead of executing tasks in chain as proposed by @pAulseperformance because the second task will be executed though the first failed. You can use callbacks to execute a task only if the previous one succeed.

add_together.apply_async((2, 2), link=subtract.si(4,2))

For more about callbacks checkout this or this for task signature

like image 183
Rukamakama Avatar answered Nov 02 '25 06:11

Rukamakama