Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to non-violently stop particular task of celery worker?

Tags:

python

celery

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.

like image 525
nicks Avatar asked May 18 '16 11:05

nicks


People also ask

How do you stop the execution of a celery task?

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.

Can a celery task call another 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".

What is soft time limit in Celery?

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.

How do you track Celery tasks?

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.


1 Answers

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

like image 119
Mauro Rocco Avatar answered Sep 19 '22 09:09

Mauro Rocco