使用web.py作为非阻塞的HTTP服务器 [英] Using web.py as non blocking http-server

查看:188
本文介绍了使用web.py作为非阻塞的HTTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习一些使用python的基本编程时,我发现了web.py.一世 卡住了一个愚蠢的问题:

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

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

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

推荐答案

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

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.

这篇关于使用web.py作为非阻塞的HTTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆