Stopping celery task gracefully

2019-05-12 18:32发布

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?

2条回答
beautiful°
2楼-- · 2019-05-12 18:45

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.

查看更多
干净又极端
3楼-- · 2019-05-12 18:48

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()
查看更多
登录 后发表回答