Cancel an already executing task with Celery?

celery revoke all tasks
restart celery
celery remove task from queue
celery task
celery get task id
celery revoke task state
celery worker
celery remote worker

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!

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

[SOLVED] Cancel an already executing task with Celery?, Celery revoke command doesn't really stop the job /8920643/cancel-an-​already-executing-task-with-celery/8924116#8924116 I already solved this issue. 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.

In Celery 3.1, the API of revoking tasks is changed.

According to the Celery FAQ, 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)

Celery revoke command doesn't really stop the job : Python, When a worker receives a revoke request it will skip executing the task, but it won​'t terminate an already executing task unless the terminate option is set. Note. app.control.revoke( [uuid for uuid, _ in celery.events.state.State().tasks_by_type(task.name)]) However, this solution will ignore those tasks that have been scheduled for future execution. Like some people who commented on other answers, when I checked what .tasks_by_type() return I had an empty list.

@0x00mh's answer is correct, however recent celery docs say that using the terminate option is "a last resort for administrators" because you may accidentally terminate another task which started executing in the meantime. Possibly a better solution is combining terminate=True with signal='SIGUSR1' (which causes the SoftTimeLimitExceeded exception to be raised in the task).

Workers Guide, Is it safe to run celery worker as root? Can I cancel the execution of a task? serialization support used to be pickle, but since 4.0 the default is now JSON. 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.

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 time of execution, then see expires argument of apply_async method.

Frequently Asked Questions, This issue is a continuation of issues #4295 (see for Celery details) and #4299. I am trying to cancel a scheduled task using revoke(), but the task still are seriously impacting productivity, and we're already investigating  This will block until the task completes, and returns a celery.result.EagerResult instance. celery.execute.apply_async(*args, **kwargs)¶ Run a task asynchronously by the celery daemon(s).

In addition, unsatisfactory, there is another way(abort task) to stop the task, but there are many unreliability, more details, see: http://docs.celeryproject.org/en/latest/reference/celery.contrib.abortable.html

Revoked task still executes? · Issue #4300 · celery/celery · GitHub, The cancel( ) method is used to stop the class from being executed. A task that is already executing is unaffected when this method is called. However, if the task  celery -A proj control revoke <task_id> All worker nodes keeps a memory of revoked task ids, either in-memory or persistent on disk (see Persistent revokes). When a worker receives a revoke request it will skip executing the task, but it won’t terminate an already executing task unless the terminate option is set.

Java Threads: Understanding and Mastering Concurrent Programming, to switch its execution to the new task before cancelling it and the programmer tasks already executing cannot be cancelled and hence they should continue  As Celery documentation states, already executing task will not be aborted by calling .revoke(), unless terminate=True is set. But that is not recommended, because it will kill the worker itself, which might have already started another task.

OpenMP in a Heterogeneous World: 8th International Workshop on , Canceling tasks involves the CancellationTokenSource and CancellationToken In other words, if a task's execution is already underway, it cannot be brutally  Tasks are either pending, finished, or waiting to be retried. Having a “started” status can be useful for when there are long running tasks and there’s a need to report what task is currently running. The host name and process id of the worker executing the task will be available in the state meta-data (e.g., result.info[‘pid’])

Pro .NET Performance: Optimize Your C# Applications, Motivation The Cancel task pattern provides the ability to withdraw a task that has been enabled or is already executing. This ensures that it will not commence  Django - Executing a task through celery from a model. Ask Question Cancel an already executing task with Celery? 12. Celery Task Chain and Accessing **kwargs. 0.

Comments
  • This is exactly the explanation I was looking for, thank you!
  • Does this work in a distributed env? I mean if I have workers on multiple machines that are executing tasks. Does celery keep track of which machine the task is executing on?
  • It does. The communication with workers takes place via the broker.
  • result.revoke(terminate=True) should do the same thing as revoke(task_id, terminate=True)
  • Also, using the terminate option is "a last resort for administrators", as per recent Celery docs. You run the risk of terminating another task which has recently started on that worker.
  • This solution worked very well for me. When SoftTimeLimitExceeded is raised in my task, my custom cleanup logic (implemented via try/except/finally) is invoked. This is much better, in my view, than what AbortableTask offers (docs.celeryproject.org/en/latest/reference/…). With the latter, you need a database result backend and you have to manually and repeatedly check the status of an ongoing task to see if it's been aborted.
  • How is this better, as far I understand if there is any other task picked up by the process, its gonna be stopped anyway, just different exception will be thrown.
  • If I use worker_prefetch_multiplier = 1 since I just have a few long running tasks the terminate should be fine - since no other tasks will be effected by terminating - did I get this correct? @spicyramen