从Django函数内部调用基本celery任务函数

2024-04-19 18:49:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在ubuntu EC2节点上有一个django项目,它执行一个计算密集的长时间运行过程,通常需要60秒以上。我需要缓存结果。{a1和}我一直在看^和^文档。我已经能够在命令行中运行一个基本任务,但是我不清楚如何从django函数内部启动任务。在

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

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

是不是很简单:

^{pr2}$

在索引函数中?在


Tags: django函数fromimporttokentaskreturnrequest