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

Question:

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.

Asked By: nicks

||

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

Answered By: Mauro Rocco

You can send HUP signal instead of TERM which gracefully restarts child process without killing worker.

In [80]: import signal

In [81]: x = add.delay(1, 2)

In [82]: x.revoke(terminate=True, signal=signal.SIGHUP)
Answered By: Chillar Anand
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.