在Celery中按ID检索任务结果 [英] Retrieve task result by id in Celery
本文介绍了在Celery中按ID检索任务结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试检索已完成任务的结果。
有效
I am trying to retreive the result of a task which has completed. This works
from proj.tasks import add
res = add.delay(3,4)
res.get()
7
res.status
'SUCCESS'
res.id
'0d4b36e3-a503-45e4-9125-cfec0a7dca30'
但是我想从另一个应用程序运行它。所以我重新运行python shell并尝试:
But I want to run this from another application. So I rerun python shell and try:
from proj.tasks import add
res = add.AsyncResult('0d4b36e3-a503-45e4-9125-cfec0a7dca30')
res.status
'PENDING'
res.get() # Error
如何检索结果?
推荐答案
使用 AsyncResult
。 (请参见此 answer )
因此,首先创建任务:
from cel.tasks import add
res = add.delay(3,4)
print(res.status) # 'SUCCESS'
print(res.id) # '432890aa-4f02-437d-aaca-1999b70efe8d'
然后启动另一个python shell:
Then start another python shell:
from celery.result import AsyncResult
from cel.tasks import app
res = AsyncResult('432890aa-4f02-437d-aaca-1999b70efe8d',app=app)
print(res.state) # 'SUCCESS'
print(res.get()) # 7
这篇关于在Celery中按ID检索任务结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文