异步背景的web2py进程

编程入门 行业动态 更新时间:2024-10-12 05:47:00
本文介绍了异步背景的web2py进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在一个叫做控制器方法里面的web2py应用程序异步处理一个大的(时间和内存消耗)的过程。

I need to to handle a large (time and memory-consuming) process asynchronously in a web2py application called inside a controller method.

我的具体使用情况是通过 stdlib.subprocess 来调用一个过程,等待它不会阻塞网络服务器退出,但我愿意接受替代方法。

My specific use case is to call a process via stdlib.subprocess and wait for it to exit without blocking the web server, but I am open to alternative methods.

  • 动手例子是一个加号。
  • 第三方库的建议都欢迎。
  • 科雷调度不需要/通缉。
推荐答案

假设你需要启动后台任务的多个可能同时出现,情况下,解决方案是一个任务队列。我听说过好东西芹菜和RabbitMQ的,如果你正在寻找第三方选项,包括web2py中的它自己的任务队列系统可能足以满足您的需求。

Assuming you'll need to start multiple, possibly simultaneous, instances of the background task, the solution is a task queue. I've heard good things about Celery and RabbitMQ, if you're looking for 3rd-party options, and web2py includes it's own task queue system that might be sufficient for your needs.

与任一工具,您将定义一个封装所需的后台进程来执行操作的功能。然后将任务队列工人联机。所述的web2py手册和论坛表明这可以在web2py的cron的系统,该系统每当Web服务器启动时触发@reboot语句来完成。可能有其他的方式来启动的工人,如果这是不能令人满意的。

With either tool, you'll define a function that encapsulates the operation you want the background process to perform. Then bring the task queue workers online. The web2py manual and forums indicate this can be done with an @reboot statement in the web2py cron system, which is triggered whenever the web server starts. There are probably other ways to start the workers if this is unsatisfactory.

在你的控制器,你会插入一个任务到任务队列中,传递任何必要的参数作为函数的输入(后台功能将无法在相同的环境中运行的控制器,所以不会有机会获得会议上,DB等,除非你明确地传递适当的值到任务功能)。

In your controller you'll insert a task into the task queue, passing any necessary parameters as inputs to the function (the background function will not run in the same environment as the controller, so it won't have access to the session, DB, etc. unless you explicitly pass the appropriate values into the task function).

现在,要获得的后台操作的输出给用户。当你插入一个任务到任务队列中,你应当得到该任务的唯一ID。然后,您可以实现调用任务队列的API来检查指定任务的状态控制器逻辑(无论是东西,希望AJAX调用,或直到任务完成,保持清新的页面)。如果任务的状态为已完成,将数据返回给用户。如果没有,继续等待。

Now, to get the output of the background operation to the user. When you insert a task into the task queue, you should get back a unique ID for the task. You would then implement controller logic (either something that expects an AJAX call, or a page that keeps refreshing until the task completes) that calls the task queue's API to check the status of the specified task. If the task's status is "finished", return the data to the user. If not, keep waiting.

更多推荐

异步背景的web2py进程

本文发布于:2023-10-23 14:14:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521058.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程   背景   web2py

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!