我遵循了 http://celeryq.org/docs/django-celery/getting-started/first-steps-with-django.html 中的指南并在tasks.py中创建了一个调用我的测试方法的 View :
import time
from celery.decorators import task
@task()
def add(x, y):
time.sleep(10)
return x + y
但是如果我的 add-method 需要很长时间才能响应,我如何存储在调用 add.delay(1,2) 时得到的结果对象并使用它来检查进度/成功/结果稍后使用 get ?
最佳答案
您只需要任务 ID:
result = add.delay(2, 2)
result.task_id
有了这个,您可以轮询任务的状态(使用例如 AJAX)
Django-celery 带有一个以 JSON 格式返回结果和状态的 View :
http://celeryq.org/docs/django-celery/reference/djcelery.views.html
关于django - 如何在 Django View 中使用 celery 存储延迟调用的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3703472/