我想优雅地退出芹菜任务(即不是通过调用
revoke(celery_task_id, terminate=True)
)。我想我应该向设置标志的任务发送一条消息,以便任务函数可以返回。与任务沟通的最佳方式是什么?
您也可以使用
AbortableTask
。我认为这是最好的方法
优雅地停止任务。
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!'
如果您将任务 ID 保存在某处,您可以随时调用它。
from celery.contrib.abortable import AbortableAsyncResult
abortable_task = AbortableAsyncResult(task_id)
abortable_task.abort()