在台式机应用程序中运行嵌入式Web服务器的推荐方法是什么(例如带有pyqt的wsgi服务器) [英] what is the recommended way of running a embedded web server within a desktop app (say wsgi server with pyqt)

查看:100
本文介绍了在台式机应用程序中运行嵌入式Web服务器的推荐方法是什么(例如带有pyqt的wsgi服务器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

桌面应用程序应在启动时启动Web服务器,并在关闭时将其关闭。

The desktop app should start the web server on launch and should shut it down on close.

假设桌面是唯一允许连接到Web服务器的客户端,那么编写此内容的最佳方法是什么?

Assuming that the desktop is the only client allowed to connect to the web server, what is the best way to write this?

Web服务器和台式机都在各自的阻塞循环中运行。因此,应该使用线程还是进行多处理?

Both the web server and the desktop run in a blocking loop of their own. So, should I be using threads or multiprocessing?

推荐答案

使用CherryPy或paste.httpserver之类的东西。您可以使用wsgiref的服务器,并且通常可以在本地正常运行,但是如果您使用的是Ajax,则wsgiref的单线程性质可能会导致一些奇怪的结果,或者如果您执行子请求,您将获得竞争条件。但是在大多数情况下都可以。 可能对您没有嵌入式线程服务器(CherryPy和paste.httpserver都是线程)很有用,在这种情况下,wsgiref会有所帮助(所有请求都将从同一线程运行)。

Use something like CherryPy or paste.httpserver. You can use wsgiref's server, and it generally works okay locally, but if you are doing Ajax the single-threaded nature of wsgiref can cause some odd results, or if you ever do a subrequest you'll get a race condition. But for most cases it'll be fine. It might be useful to you not to have an embedded threaded server (both CherryPy and paste.httpserver are threaded), in which case wsgiref would be helpful (all requests will run from the same thread).

请注意,如果您使用CherryPy或paste.httpserver,所有请求将自动在子线程中发生(那些包会为您生成线程),并且您可能无法直接从您的Web代码中触摸GUI代码(因为GUI代码通常不喜欢由线程处理)。对于它们中的任何一个,服务器代码块都是如此,因此您需要产生一个线程来启动服务器。Twisted可以在常规的GUI事件循环中运行,但是除非如此重要,否则它会增加很多复杂性。

Note that if you use CherryPy or paste.httpserver all requests will automatically happen in subthreads (those packages do the thread spawning for you), and you probably will not be able to directly touch the GUI code from your web code (since GUI code usually doesn't like to be handled by threads). For any of them the server code blocks, so you need to spawn a thread to start the server in. Twisted can run in your normal GUI event loop, but unless that's important it adds a lot of complexity.

不要使用BaseHTTPServer或SimpleHTTPServer,它们很愚蠢且复杂,在 all 情况下,您可能会使用wsgiref。每一种情况,例如wsgiref都具有健全的API(WSGI),而这些服务器具有愚蠢的API。

Do not use BaseHTTPServer or SimpleHTTPServer, they are silly and complicated and in all cases where you might use then you should use wsgiref instead. Every single case, as wsgiref is has a sane API (WSGI) while these servers have silly APIs.

这篇关于在台式机应用程序中运行嵌入式Web服务器的推荐方法是什么(例如带有pyqt的wsgi服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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