对于这个 Python 脚本来说,最好的(或合适的)WSGI 服务器是什么? - Python
我在决定如何提供一些 Python 脚本时遇到了很大的问题。
问题是基本功能可以这样概括:
do_something()
time.sleep(3)
do_something()
我尝试了各种 WSGI 服务器,但它们都给了我并发限制,比如我必须指定要使用多少个线程等等。
我只希望服务器上的资源能够高效、自由地使用。
有什么想法吗?
I'm having quite a problem deciding how to serve a few Python scripts.
The problem is that the basic functionality could be generalized by this:
do_something()
time.sleep(3)
do_something()
I tried various WSGI servers, but they have all been giving me concurrency limitations, as in I have to specify how many threads to use and so on.
I only wish that the resources on the server be used efficiently and liberally.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您检查过龙卷风及其非阻塞异步请求吗?
http://www.tornadoweb.org/
虽然我从未使用过它,但这是来自文档的示例:
Have you checked tornado with its non-blocking asynchronous requests?
http://www.tornadoweb.org/
I have never used it though but here is an example from doc:
您可能会发现 Spawning 很合适。它有多种部署选项,其中之一是透明的异步(由 Eventlet 实现)。因此,如果您确实执行
time.sleep(3)
那就没问题了。并非您可能做的所有事情都是透明处理的,因此您必须注意 Eventlet 及其工作原理。例如,套接字是这样的,因此如果您从套接字读取(并且该套接字阻塞),它不会停止服务器或消耗线程。但如果您执行 CPU 密集型工作,则会阻止所有请求。所以...有点棘手。 Spawning 还有一些其他可能也适合您的部署选项。您也许可以使用 WaitForIt,尽管它有一些问题。它将为长时间运行的请求生成线程,并提供一些浏览器反馈,因此如果您要为长时间运行的后端进程创建一个非常简单的前端,它可能会很有用。它充当 WSGI 中间件。
You might find Spawning a good fit. It has several options for deployment, one of which is somewhat transparent async (as implemented by Eventlet). So if you literally do
time.sleep(3)
it'll be okay. Not everything you might do is transparently handled, so you have to pay some attention to Eventlet and how it works. Sockets are, for instance, so if you read from a socket (and that socket blocks) it won't halt the server or consume a thread. But if you do CPU-heavy work that will block all requests. So... it's a bit tricky. Spawning has some other deployment options that might work for you too.You might be able to use WaitForIt, though it has some gotchas. It will spawn threads for long-running requests, and provides some browser feedback, so if you are creating a very simplistic frontend to long-running backend processes it might be useful. It acts as WSGI middleware.
CherryPy WSGI 服务器怎么样?
睡眠
是什么意思?您真的在编写 Web 应用程序吗?What about CherryPy WSGI server?
What does that
sleep
mean? Are you really writing a web application?那么客户端等待应答 3 秒是可以的,但服务器就不行了?这看起来……很奇怪。
如果您不希望客户端被占用 3 秒,常见的机制是让初始请求尽快返回“202 Accepted”,并提供指向状态监视器的 URL。然后服务器可以为该任务生成一个新的线程或子进程,而客户端可以做其他事情,然后轮询状态 URL 以了解任务何时完成。
So it's OK for the client to be tied up waiting for an answer for 3 seconds, but not OK for the server? That seems...odd.
If you'd rather not have the client tied up for 3 seconds, a common mechanism is to have the initial request return "202 Accepted" ASAP with a URL to a status monitor. Then the server can spawn a new thread or subprocess for the task, and the client can do other things, and then poll the status URL to find out when the task is done.