【发布时间】:2015-11-14 13:47:54
【问题描述】:
#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>
为了简单起见,我删除了很多代码,但这就是它的要点。尽管我为 my_func 发布了代码,但它是一个耗时的函数,它会导致 index.html 在被访问时加载一段时间。如何使用 celery 和 redis 在后台运行 my_func 以便 index.html 加载更快?
我已阅读 celery 文档,但我仍然无法设置 celery 和 redis。谢谢。
【问题讨论】:
-
这样:context = { "pi1" : pi1, "pis1" : pis1, } 工作吗?
-
一切正常,只是速度很慢。
标签: python django asynchronous redis celery