Retrieve task result by id in Celery

馋奶兔 提交于 2019-11-27 15:37:31

问题


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'

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

How can I retrieve the result?


回答1:


It works using AsyncResult. (see this answer)

So first create the task:

from cel.tasks import add
res = add.delay(3,4)
res.status
'SUCCESS'
res.id
'432890aa-4f02-437d-aaca-1999b70efe8d'

Then start another python shell:

from celery.result import AsyncResult
from cel.tasks import app
res = AsyncResult('432890aa-4f02-437d-aaca-1999b70efe8d',app=app)
res.state
'SUCCESS'
res.get()
7



回答2:


This is due to RabbitMQ not actually storing the results. If you need the ability to get the results later on, use redis or SQL as the result backend.



来源:https://stackoverflow.com/questions/30753040/retrieve-task-result-by-id-in-celery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!