Retrieve task result by id in Celery

后端 未结 2 1736
无人及你
无人及你 2020-12-09 01:28

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         


        
相关标签:
2条回答
  • 2020-12-09 01:58

    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.

    0 讨论(0)
  • 2020-12-09 02:14

    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
    
    0 讨论(0)
提交回复
热议问题