django - 如何在 Django View 中使用 celery 存储延迟调用的结果?

标签 django view celery

我遵循了 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/

相关文章:

c# - 如何在WPF MVVM中将View对象发送到ViewModel?

python - Django 模板 : accessing the previous and the following element of the list

django - 如何在 Django 迁移中包含 PostgreSQL 函数?

python - Django 社交授权

css - Ember.js 模板和全高(包括 JS bin 示例)

android - 整个屏幕的onTouchListener

django - 如何修复 "Error Removing corrupted schedule file ' celerybeat-schedule' : error ('Bad magic number' , )r"

django - 我应该使用 attach-daemon 还是 smart-attach daemon 用 UWSGI 自动启动 celery(并轻松更新任务)

python - 如何在 ModelForm 中使用 SplitArrayField

python - Flask 和减少 mysql 查询的最佳方法,也许是 celery?