- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
#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。谢谢。
最佳答案
如前所述,您可能不需要 celery 。这是从案例 2 派生的示例:https://zapier.com/blog/async-celery-example-why-and-how/ .它完全适合我:
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 包含:
<!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 包含:
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)
]
当我访问 localhost:8000/test/时发生的事情是我立即看到:
大约 10 秒后,我看到:
想法是您立即返回您的页面并使用 jquery 在操作完成时获取操作结果并相应地更新您的页面。您可以添加更多内容,例如进度条/加载图像等。对于您的示例,您可以在后台对 pi1
和 pis
进行处理,然后将其加载到 HTML 中就这样结束了。
关于python - Django - 如何在 celery 和 redis 中使用异步任务队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32126366/
当我运行此命令进行 celery 节拍时。 [2013-06-27 02:17:05,936: INFO/MainProcess] Celerybeat: Starting... [2013-06-2
我需要构建一个处理两种类型任务的系统。一种类型可以创建更多自身或另一种类型的任务。将有很少的 worker (2-3)和只有一个主机。最重要的要求是系统应该优雅地处理重新启动:即在重新启动时,正在进行
我们使用 Celery 4.2.1 和 Redis,并为我们的任务设置了全局软超时和硬超时。我们所有的自定义任务都设计为保持在限制范围内,但每天内置任务 backend_cleanup 任务最终都会因
我知道这违背了使用 Celery 的全部目的,但是是否有一个函数会阻塞直到结果返回? 所以我可以调用 actual_result = MyTask.dont_delay(some_arg="foo")
我们使用 Celery 4.2.1 和 Redis,并为我们的任务设置了全局软超时和硬超时。我们所有的自定义任务都设计为保持在限制范围内,但每天内置任务 backend_cleanup 任务最终都会因
我知道这违背了使用 Celery 的全部目的,但是是否有一个函数会阻塞直到结果返回? 所以我可以调用 actual_result = MyTask.dont_delay(some_arg="foo")
我计划使用 celery 作为我的项目的任务管理组件。它几乎具有我的项目所需的所有功能。我将有一组可以独立执行或按指定顺序执行的任务。在顺序任务中,我希望能够在中间任务之一失败时执行清理/回滚。我想知
它是运行 Celery 的实际处理器还是另一个进程?在花中,我可以在工作池中看到多个进程吗?这两者之间有什么区别? 最佳答案 当您运行 celery worker 时,它会创建一个父进程来管理正在运行
我有一个名为 ShippingApp 的项目,我按照步骤设置了 celery worker。我将 celery 3.1.26.post2 与 python3.7 一起使用,当我想启动 Celery W
尽我所能,我无法杀死这些 celery worker 。 我跑: celery --app=my_app._celery:app status 我看到我有3个(我不明白为什么3个 worker = 2
我在 docker 容器中运行了 celery ,我想检查选项 CELERY_TASK_RESULT_EXPIRES = '3600' 是否已应用。 我尝试使用 celery inspect conf
我使用 celery.chord(...) 创建一组任务和一个方法,该方法在组中的所有任务完成后被调用。 我使用 amqp 结果后端(但我想切换到 memcached)。 我的 worker 每秒钟一
我正在寻找一些关于将任务生成的列表映射到 celery 中的另一个任务的最佳方法的建议。 假设我有一个名为 parse 的任务,它解析 PDF 文档并输出页面列表。然后,每个页面都需要单独传递给另一个
这不是关于如何捕获 celery worker 日志的问题。有什么方法可以捕获生产者上的 celery 日志记录。我想要的是捕获当我调用 task.delay(...) 或 task.apply_as
我正在使用以下版本: 花==0.9.3 celery ==4.3.0 这为我提供了包含多个列的任务页面的以下显示: 每次我进入这个页面时,我最终都会重新排列页面,使列的顺序不同,并将行的顺序更改为降序
我想完成这样的事情: results = [] for i in range(N): data = generate_data_slowly() res = tasks.process
我想运行一个由beat 调度的复杂任务。让我们假设定义了默认的 add/mul 任务。 @app.on_after_configure.connect def setup_periodic_tasks
我有一个应用程序,其中包含 celery worker 。当我部署这将杀死那些正在运行的进程。 所以任务将开始,但永远不会完成,并且在部署完成时不会重新启动。 避免此问题并在部署完成后重新启动这些任务
我正在开始使用 Celery 进行 Django 项目。出于本地开发目的,我根据这些说明使用 djcelery 和 djkombu(数据库传输)进行了设置 http://ask.github.com/
如何配置 celery 在任务失败时发送电子邮件警报? 例如,我希望 Celery 在 3 个以上的任务失败或 10 个以上的任务被重试时通知我。 是否可以使用 celery 或实用程序(例如花),或
我是一名优秀的程序员,十分优秀!