Django的 - 如何使用异步任务队列,芹菜和Redis的 [英] Django - How to use asynchronous task queue with celery and redis

查看:161
本文介绍了Django的 - 如何使用异步任务队列,芹菜和Redis的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#In my views.py file
pi1 = None
pis1 = None
def my_func():
    #Essentially this function sets a random integer to pi1 and pis1
    global pi1, pis1
    pi1 = randint(0,9)
    pis1 = randint(0,9)
    return        

def index(request):

    my_func()

    context = {
        "pi1" : pi1,
        "pis1" : pis1,
    }

    return render(request, "index.html", context)

#In the index.html file
<h1>{{ pi1 }}</h1>
<h1>{{ pis1 }}</h1>

我已经删除了很多我的code为简单的,但是这是它的要点。尽管我已经张贴了my_func,并将在code,它是导致的index.html,在访问加载一段时间耗时的功能。我将如何运行的底色my_func,并将使用芹菜和Redis的,这样的index.html载入速度更快?

I've removed a lot of my code for simplicity, but this is the gist of it. Despite the code that I've posted for my_func, it is a time consuming function that causes index.html to load for awhile when it is accessed. How would I run my_func in the backround using celery and redis so that index.html loads more quickly?

我读过芹菜文档,但我仍然有问题设立芹菜Redis的。谢谢你。

I've read the celery documentation, but I am still having trouble setting up celery and redis. Thank you.

推荐答案

由于据说previous,你可能不需要芹菜。下面是这种情况下,2派生一个例子: https://开头zapier。 COM /博客/异步芹菜,例如,为什么和技术/ 。它完全为我工作:

As is said previous, you might not need celery. Here's an example derived from case 2 of this: https://zapier.com/blog/async-celery-example-why-and-how/. It's fully working for me:

from time import sleep
import json
from django.http import HttpResponse
from django.shortcuts import render

def main_view(request):
    return render(request, 'index.html')

def ajax_view(request):
    sleep(10) #This is whatever work you need
    pi1 = "This is pi1" #I just made pi1/pis1 random values
    pis1 = "This is pis1"
    context = {
        "pi1" : pi1,
        "pis1" : pis1,
    }
    data = json.dumps(context)

    return HttpResponse(data, content_type='application/json')

我的index.html包括:

My index.html contains:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Main View</title>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script>
    $(document).ready(function(){
        $.ajax({
            url: "/test_ajax/",
        }).done(function( data) {
            $("#pi1").text(data.pi1);
            $("#pis1").text(data.pis1); 
        });
    });
</script>
  </head>
  <body>
      <h1 id = "pi1">Loading</h1>
      <h1 id = "pis1">Loading</h1>
  </body>
</html>

和我的urls.py包括:

And my urls.py contains:

from django.conf.urls import include, url
from django.contrib import admin
from testDjango.test import main_view, ajax_view

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^test/', main_view),
    url(r'^test_ajax/', ajax_view)
]

什么当我访问本地主机发生:8000 /测试/是我的立即查看:

What happens when I visit localhost:8000/test/ is that I instantly see:

初诊

大约10秒钟之后,我再看看:

After about 10 seconds, I then see:

我们的想法是,你立刻返回页面并使用jQuery每当完成的获取操作的结果,并相应地更新页面。您可以添加更多的东西像进度条/加载图像等。你的榜样,你可以为 PI1 PIS 在后台而这完成之后将其加载到HTML。

The idea is that you return your page instantly and use jquery to fetch the result of the operation whenever that's finished and update your page accordingly. You can add more things like progress bars/loading image etc. For your example, you can do the processing for pi1 and pis in the background and load it into the HTML after that's finished.

这篇关于Django的 - 如何使用异步任务队列,芹菜和Redis的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆