Retrieve result from 'task_id' in Celery from unknown task

陌路散爱 提交于 2019-12-04 04:33:13

Ok - so I've been looking for a solution for a long time, and now that I've finally formally posted this and looked over the documentation I found this gem:

class celery.result.AsyncResult(id, backend=None, task_name=None, app=None, parent=None)

Query task state.

Parameters:

id – see id.

backend – see backend.

exception TimeoutError

Error raised for timeouts.

AsyncResult.app = None

So instead of providing the backend parameter I provided the "app" argument instead like so:

from celery.result import AsyncResult
from task import app

# Assuming add.delay(10,10) was called in another process
# and that I'm using a 'task_id' I retrieved from that process

result = AsyncResult(id='copied_task_id', app=app)
result.state # 'SUCCESSFUL'
result.get() # 20

This is probably obvious to many. It wasn't to me. For now all I can say is that this solution "just works", but I'd feel more comfortable if I knew it was the sanctioned way to do it. If you know of a section in the documentation that makes this more clear please post it in the comments or as an answer and I'll select it as the answer if I can.

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