如何在django视图中使用芹菜存储延迟呼叫的结果? [英] How to store the result of a delay-call using celery in a django view?
问题描述
我遵循 http ://celeryq.org/docs/django-celery/getting-started/first-steps-with-django.html ,并创建了一个在tasks.py中调用我的测试方法的视图:
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
但是如果我的add-method需要很长时间的响应,那么如何在调用add.delay(1,2)时存储结果对象,并使用它来检查进度/成功/结果? / p>
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?
推荐答案
您只需要task-id:
You only need the task-id:
result = add.delay(2, 2)
result.task_id
使用这个你可以轮询任务的状态(使用例如AJAX)
Django-celery附带了一个返回结果和JSON的状态的视图:
http://celeryq.org/docs/django-celery /reference/djcelery.views.html
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
这篇关于如何在django视图中使用芹菜存储延迟呼叫的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!