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. Does that mean that there is no reliable, stable way to do that?
EDIT: celery.contrib.abortable
doesn't suit me, because, as documentation states, it works only with database backends.
To cancel an already executing task with Celery and Python, we can use the revoke function. to call revoke with the task_id of the task to stop. And we set terminate to True to terminate the task.
To answer your opening questions: As of version 2.0, Celery provides an easy way to start tasks from other tasks. What you are calling "secondary tasks" are what it calls "subtasks".
Celery worker is set to timeout after 10 seconds with soft timeout of 5 seconds. A delay is introduced to the celery task in order to cause a timeout. The timeout Exception is never raised by Celery worker.
Keep the ID in your client app, open a socket.io channel to listen for updates. The celery task sends messages to Redis, this will trigger socket.io events. Socket.io relays the messages to the browser, in real time.
A running task is a running subprocess of the worker (when using prefork), this means that the only way to abort a task is to kill the subprocess that is running it.
You may try to experiment your own implementation of revoke event handling trying to figure out the subprocess ID and kill only that one, but honestly don't know if is worth and if it can really work.
I think that short answer is you can't.
Anyway killing the worker is needed sometimes, especially in initial phases of projects where you still need to dimension correctly the resources, just make sure you log somewhere the running tasks so you can reschedule them or just use CELERY_ACKS_LATE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With