【问题标题】:How to query status of celery task如何查询芹菜任务的状态
【发布时间】:2020-11-22 18:38:02
【问题描述】:

通过这段代码,是否可以查询worker state是在state=1还是state=2?

from celery import Celery
import time

#celery -A CeleryTask worker --loglevel=info

app = Celery("CeleryTask", backend="redis://localhost", broker="redis://localhost")


@app.task
def train():
    for i in range(100):
        if i<5:
            state=1

        else:
            state=2
        time.sleep(10)

    return "hallo"

if __name__ == '__main__':
    result = train.delay()
  

【问题讨论】:

    标签: python python-3.x celery celery-task


    【解决方案1】:

    Celery 确实很棒,它为您提供了创建自己的 custom states 并使用 update_state() 方法更新它们的机制。

    来自(链接的)文档:

    @app.task(bind=True)
    def upload_files(self, filenames):
        for i, file in enumerate(filenames):
            if not self.request.called_directly:
                self.update_state(state='PROGRESS',
                    meta={'current': i, 'total': len(filenames)})
    

    在您的情况下,您只需使用 meta={"state":X} 调用 update_state(),其中 X 为 1 或 2...

    【讨论】:

      猜你喜欢
      • 2012-02-06
      • 2012-11-16
      • 2014-07-31
      • 2013-10-07
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多