Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django函数内部的基本芹菜任务函数调用_Python_Django_Celery - Fatal编程技术网

Python django函数内部的基本芹菜任务函数调用

Python django函数内部的基本芹菜任务函数调用,python,django,celery,Python,Django,Celery,我在ubuntu EC2节点上有一个django项目,它执行一个计算密集的长时间运行过程,通常需要60秒以上。我需要缓存结果。我一直在阅读和阅读文档。我已经能够在命令行中完成一项基本任务,但我不清楚如何从django函数内部启动任务 现在,我在django视图中的代码结构是: from django.shortcuts import render from django.http import HttpResponse from django.views.decorators.csrf impo

我在ubuntu EC2节点上有一个django项目,它执行一个计算密集的长时间运行过程,通常需要60秒以上。我需要缓存结果。我一直在阅读和阅读文档。我已经能够在命令行中完成一项基本任务,但我不清楚如何从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
它是否像呼叫一样简单:

calculator(token)

在索引函数中?

几乎和您所说的一样简单:

calculator.apply_async()


有关更多详细信息,请参阅如何调用任务。

谢谢,这很有帮助!!
calculator.delay()