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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
回答2:
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()