Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

Retrieve task result by id in Celery

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?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It works using AsyncResult. (see this answer)

So first create the task:

from cel.tasks import add

res = add.delay(3,4)
print(res.status) # 'SUCCESS'
print(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)

print(res.state) # 'SUCCESS'
print(res.get()) # 7

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...