使用 web.py 作为非阻塞 http 服务器

发布于 2024-07-12 12:58:22 字数 417 浏览 7 评论 0原文

在学习一些基本的 python 编程时,我发现了 web.py。 我 遇到了一个愚蠢的问题:

我编写了一个简单的控制台应用程序,其中包含一个处理项目的主循环 来自单独线程中的队列。 我的目标是使用 web.py 添加 将项目添加到我的队列并通过 Web 请求报告队列状态。 我 将此作为模块运行,但无法将其集成到我的主应用程序中。 我的问题是当我用 app.run() 启动 http 服务器时它会阻止我 主循环。 也尝试用 thread.start_new_thread 启动它,但它仍然 块。 有没有一种简单的方法来运行 web.py 的集成 http 服务器 我的应用程序中的背景。

万一我是根本性事件的受害者 误解,任何试图澄清我的推理错误的尝试都会 帮助;.)(请耐心等待,我是初学者:-)

while learning some basic programming with python, i found web.py. i
got stuck with a stupid problem:

i wrote a simple console app with a main loop that proccesses items
from a queue in seperate threads. my goal is to use web.py to add
items to my queue and report status of the queue via web request. i
got this running as a module but can´t integrate it into my main app.
my problem is when i start the http server with app.run() it blocks my
main loop.
also tried to start it with thread.start_new_thread but it still
blocks.
is there an easy way to run web.py´s integrated http server in the
background within my app.

in the likely event that i am a victim of a fundamental
missunderstanding, any attempt to clarify my error in reasoning would
help ;.) ( please bear with me, i am a beginner :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨离汐 2024-07-19 12:58:22

我找到了一个可行的解决方案。 在一个单独的模块中,我创建了我的网络服务器:

import web
import threading
class MyWebserver(threading.Thread):

    def run (self):
        urls = ('/', 'MyWebserver')
        app = web.application(urls, globals())
        app.run()

    def POST ...

在主程序中,我只是调用

MyWebserver().start()

,然后继续执行我想要的任何操作,同时让网络服务器在后台工作。

I found a working solution. In a seperate module i create my webserver:

import web
import threading
class MyWebserver(threading.Thread):

    def run (self):
        urls = ('/', 'MyWebserver')
        app = web.application(urls, globals())
        app.run()

    def POST ...

In the main programm i just call

MyWebserver().start()

and than go on with whatever i want while having the webserver working in the background.

纸短情长 2024-07-19 12:58:22

将主循环代码重写为您一遍又一遍调用的函数,然后从传递给 runsimple 的函数中调用它,这不是更简单吗

?保证不能完全满足您的要求,但如果您很着急,这可能是最简单的。

Wouldn't is be simpler to re-write your main-loop code to be a function that you call over and over again, and then call that from the function that you pass to runsimple...

It's guaranteed not to fully satisfy your requirements, but if you're in a rush, it might be easiest.

多彩岁月 2024-07-19 12:58:22

或者只使用 Tornado,一个用于 Python 的非阻塞网络服务器,其 API 类似于 webpy -
http://www.tornadoweb.org/

or just use Tornado, a non-blocking webserver for Python that has an API similar to webpy -
http://www.tornadoweb.org/

战皆罪 2024-07-19 12:58:22

我最近还使用 Beanstalkd 对将在单独线程中运行的任务进行排队。 您的 web.py 处理程序只是将一个作业放入管道中,然后由一个完全独立的脚本执行它。 您可以拥有任意数量的这些,并且您可以获得高级队列控制等的好处。

I have also recently used Beanstalkd to queue up tasks that will run in a separate thread. Your web.py handler just drops a job into a pipe and a completely separate script executes it. You could have any number of these, and you get the benefits of advanced queue control, etc..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文