等待芹菜完成任务
#python #celery

部署Python应用程序时,您有时必须重新启动芹菜。如果有一项长期运行的任务,您可能不想杀死该任务,而是要等到任务完成。

此功能将暂停接受新任务并等待'所有任务完成。

from celery import Celery
from celery.app.control import Control

def celery_check():
    app = Celery("app") # Name of the main module
    control = Control(app)
    control.cancel_consumer("celery")  # queue name, must probably be specified once per queue, but we use a single queue
    inspect = control.inspect()
    while True:
        active = inspect.active()
        if len(active.items()) > 0:
            seconds = 10
            time.sleep(seconds)
        else:
            break