How to store the result of a delay-call using celery in a django view?

和自甴很熟 提交于 2019-12-04 23:06:50

问题


I`ve followed the guidelines in http://celeryq.org/docs/django-celery/getting-started/first-steps-with-django.html and created a view that calls my test method in tasks.py:

import time
from celery.decorators import task

@task()
def add(x, y):
    time.sleep(10)
    return x + y

But if my add-method takes a long time to respond, how can I store the result-object I got when calling add.delay(1,2) and use it to check the progress/success/result using get later?


回答1:


You only need the task-id:

result = add.delay(2, 2)
result.task_id

With this you can poll the status of the task (using e.g. AJAX) Django-celery comes with a view that returns results and status in JSON: http://celeryq.org/docs/django-celery/reference/djcelery.views.html



来源:https://stackoverflow.com/questions/3703472/how-to-store-the-result-of-a-delay-call-using-celery-in-a-django-view

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