nix
nix

Reputation: 2285

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

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.

Upvotes: 16

Views: 2814

Answers (2)

Mauro Rocco
Mauro Rocco

Reputation: 5128

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

Upvotes: 3

Chillar Anand
Chillar Anand

Reputation: 29514

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)

Upvotes: 1

Related Questions