python - 如何查询芹菜任务的状态
问题描述
使用这段代码,是否可以查询workerstate
是在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()
解决方案
Celery 非常棒,它为您提供了创建自己的自定义状态并使用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)})
在您的情况下,您所要做的就是调用 update_state() ,meta={"state":X}
其中 X 为 1 或 2 ...
推荐阅读
- java - Springboot 2.2迁移给出flyway错误:发现多个版本的迁移
- google-maps - Typeform集成谷歌地图地点建议api
- java - 如何将数组分成不同的“mags”
- c++ - 在 Cocos2dx 中初始化粒子效果后的警告 - “libpng 警告:iCCP:已知不正确的 sRGB 配置文件”
- c++ - 如何形成对非最终类的“最终”引用?
- python - 在 python socketio RuntimeWarning 中:尽管编写了等待语句,但从未等待协程'initial'
- asp.net-mvc - 如何在不刷新 ASP.Net MVC 页面的情况下更新会话值
- c# - 在 iOS 的 Xamarin 表单中创建 HttpClient
- c# - 文本文件中 autovue.classpath= 之后完整字符串的正则表达式
- excel - 使用 NEXT 函数仅在定义的工作表中插入值