我在 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)
在索引函数中?
最佳答案
关于python - 从 django 函数内部调用基本 celery 任务函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29235256/