Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel an already executing task with Celery?

I have been reading the doc and searching but cannot seem to find a straight answer:

Can you cancel an already executing task? (as in the task has started, takes a while, and half way through it needs to be cancelled)

I found this from the doc at Celery FAQ

>>> result = add.apply_async(args=[2, 2], countdown=120) >>> result.revoke() 

But I am unclear if this will cancel queued tasks or if it will kill a running process on a worker. Thanks for any light you can shed!

like image 778
dcoffey3296 Avatar asked Jan 19 '12 03:01

dcoffey3296


People also ask

How do you stop the execution of a celery task?

If a task is revoked, the workers ignore the task and do not execute it. If you don't use persistent revokes your task can be executed after worker's restart. revoke has an terminate option which is False by default. If you need to kill the executing task you need to set terminate to True.

How do you clear the Celery queue?

Best method I found was redis-cli KEYS "celery*" | xargs redis-cli DEL which worked for me. This will wipe out all tasks stored on the redis backend you're using.


1 Answers

revoke cancels the task execution. If a task is revoked, the workers ignore the task and do not execute it. If you don't use persistent revokes your task can be executed after worker's restart.

http://docs.celeryproject.org/en/latest/userguide/workers.html#worker-persistent-revokes

revoke has an terminate option which is False by default. If you need to kill the executing task you need to set terminate to True.

>>> from celery.task.control import revoke >>> revoke(task_id, terminate=True) 

http://docs.celeryproject.org/en/latest/userguide/workers.html#revoke-revoking-tasks

like image 143
mher Avatar answered Sep 28 '22 03:09

mher