Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping celery task gracefully

I'd like to quit a celery task gracefully (i.e. not by calling revoke(celery_task_id, terminate=True)). I thought I'd send a message to the task that sets a flag, so that the task function can return. What's the best way to communicate with a task?

like image 942
orange Avatar asked May 11 '13 03:05

orange


2 Answers

Use signals for this. Celery's revoke is the right choice; it uses SIGTERM by default, but you can specify another using the signal argument, if you prefer.

Just set a signal handler for it in your task (using the signal module) that terminates the task gracefully.

like image 97
Cairnarvon Avatar answered Sep 28 '22 13:09

Cairnarvon


Also you can use an AbortableTask. I think this is the best way to stop task gracefully.

http://docs.celeryproject.org/en/latest/reference/celery.contrib.abortable.html

from celery.contrib.abortable import AbortableTask
from proj.celery import app

@app.task(bind=True, base=AbortableTask)
def abortable_task(self):
    while not self.is_aborted():
       print 'I am running'

    print 'I was aborted!'

If you save id task in somewhere you can call this whenever you want.

from celery.contrib.abortable import AbortableAsyncResult

abortable_task = AbortableAsyncResult(task_id)
abortable_task.abort()
like image 26
Antonio Cabanas Avatar answered Sep 28 '22 13:09

Antonio Cabanas