在 Celery 中通过 id 检索任务结果
Retrieve task result by id in Celery
我正在尝试检索已完成任务的结果。
这个有效
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 并尝试:
from proj.tasks import add
res = add.AsyncResult('0d4b36e3-a503-45e4-9125-cfec0a7dca30')
res.status
'PENDING'
res.get() # Error
如何检索结果?
这是由于 RabbitMQ not actually storing the results。如果您需要稍后获得结果的能力,请使用 redis 或 SQL 作为结果后端。
它使用 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:
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
我正在尝试检索已完成任务的结果。 这个有效
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 并尝试:
from proj.tasks import add
res = add.AsyncResult('0d4b36e3-a503-45e4-9125-cfec0a7dca30')
res.status
'PENDING'
res.get() # Error
如何检索结果?
这是由于 RabbitMQ not actually storing the results。如果您需要稍后获得结果的能力,请使用 redis 或 SQL 作为结果后端。
它使用 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:
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