python - 从 django 函数内部调用基本 celery 任务函数

标签 python django celery

我在 ubuntu EC2 节点上有一个 django 项目,该项目执行计算密集型的长时间运行过程,通常需要 60 秒以上。我需要缓存结果。我一直在读http://www.caktusgroup.com/blog/2014/06/23/scheduling-tasks-celery/http://michal.karzynski.pl/blog/2014/05/18/setting-up-an-asynchronous-task-queue-for-django-using-celery-redis/与文档一起。我已经能够在命令行上执行基本任务,但我不清楚如何从 django 函数内部启动任务。

现在我的 django View 中的代码结构是:

from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from __future__ import absolute_import
from celery import shared_task

@csrf_exempt
def index(request):

    token = str(request.POST.get('token', False))
    calculator(token)
    return HttpResponse(token)

@shared_task
def calculator(token):

    # do calculation
    # store result in cache

    return

是否像调用一样简单:

calculator(token)

在索引函数中?

最佳答案

几乎像你说的那么简单:

calculator.apply_async()

或者

calculator.delay()

参见the docs有关如何调用任务的更多详细信息。

关于python - 从 django 函数内部调用基本 celery 任务函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29235256/

相关文章:

python - python虚拟环境的问题

rabbitmq - Celery RabbitMQ 代理故障转移连接问题

python - Elasticsearch-dsl 嵌套查询

python - 是否可以对 celery 定期任务运行单元测试?

django - 守护进程 celery 进程 celeryd-multi 未找到

python - 尽管 __debug__ 为真,但条件 __debug__ 语句未执行

python - 关于正则表达式

Python:如何从给定的键中获取最近的键?

python - pandas - 要列出到字典的字符串

python - Django + PostgreSQL - 1000 次插入/秒,如何加速?