Cancel an existing task with Celery?

I read the document and searched, but cannot find a direct answer:

Is it possible to cancel an already running task? (as when starting a task, it takes some time, and halfway through it you need to cancel)

I found this from the celery FAQ

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

But I do not know if this will cancel the tasks in the queue or if it will kill the workflow of the worker. Thanks for any light you can throw off!

+73
python django celery message-passing
Jan 19 '12 at 3:21
source share
5 answers

cancels the cancellation of the task. If the task is canceled, workers ignore the task and do not complete it. If you do not use persistent revocations, your task may be completed after the worker restarts.

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

Cancel has a completion parameter, which by default is False. If you need to kill an executable task, you need to set the completion 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

+135
Jan 19 2018-12-12T00:
source share

Celery 3.1 changed the task recall API .

According to Frequently Asked Questions for the celery , you should use result.revoke:

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

or if you only have the task id:

 >>> from proj.celery import app >>> app.control.revoke(task_id) 
+28
Aug 29 '14 at 3:44 on
source share

@ 0x00mh the answer is correct, however the recent celery docs says that using the terminate option is a โ€œlast resort for administratorsโ€ because you might accidentally terminate another task that started to run in the meantime. Perhaps the best solution is to combine terminate=True with signal='SIGUSR1' (which raises the SoftTimeLimitExceeded exception in the task).

+15
Apr 14 '15 at 12:38
source share

See the following options for tasks: time_limit , soft_time_limit (or you can set it for workers). If you want to control not only the execution time, then see the expires argument of the apply_async method.

0
Jan 19 '12 at 6:40
source share

In addition, unsatisfactorily, there is another way (to abort the task) to stop the task, but there is a lot of unreliability, more detailed information: http://docs.celeryproject.org/en/latest/reference/celery.contrib.abortable.html

0
Dec 6 '18 at 6:29
source share



All Articles