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

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

问题描述

 #在我的views.py文件
pi1 =无
pis1 =无
def my_func():
#本质上这个功能将一个随机整数设为pi1和pis1
全局pi1,pis1
pi1 = randint(0,9)
pis1 = randint(0,9)
返回

def index(request):

my_func()

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

return render(request,index.html,context)

#在index.html文件
< h1> {{pi1}}< / h1>
< h1> {{pis1}}< / h1>

为了简单起见,我已经删除了很多代码,但这是它的要点。尽管我为my_func发布的代码,它是一个耗时的函数,导致index.html在访问时加载一段时间。如何使用芹菜和redis在backround中运行my_func,以便index.html更快地加载?



我已经阅读了芹菜文档,但是我仍然遇到麻烦设置芹菜和redis。谢谢。

解决方案

如前所述,您可能不需要芹菜。以下是案例2的一个示例:



约10秒钟后,我会看到:





这个想法是您即时返回页面,并使用jquery在操作结束后,相应地更新您的页面。您可以添加更多的内容,例如进度条/加载图像等。对于您的示例,您可以执行 pi1 pis 在后台完成后将其加载到HTML中。


#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>

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?

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

解决方案

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')

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>

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)
]

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

After about 10 seconds, I then see:

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天全站免登陆