Python 如何查询芹菜任务的状态

Python 如何查询芹菜任务的状态,python,python-3.x,celery,celery-task,Python,Python 3.x,Celery,Celery Task,使用此代码,是否可以查询工作人员的状态是否处于状态=1或状态=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 tr

使用此代码,是否可以查询工作人员的
状态
是否处于
状态
=1或
状态
=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()
  
从芹菜进口芹菜
导入时间
#芹菜-芹菜任务工作者--loglevel=info
应用程序=芹菜(“芹菜任务”,后端=”redis://localhost“,经纪人=”redis://localhost")
@应用程序任务
def序列():
对于范围(100)内的i:

if i芹菜,尽管它很棒,但它为您提供了创建自己的并使用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…

芹菜,尽管它很棒,但它为您提供了创建自己的状态并使用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