Python 我可以单独使用 Flask app.run() 为多个客户端提供服务吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14814201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I serve multiple clients using just Flask app.run() as standalone?
提问by ATOzTOA
I know I can link Flask with Apache or other web servers. But, I was thinking of running Flask as a standalone server serving multiple clients simultaneously.
我知道我可以将 Flask 与 Apache 或其他 Web 服务器链接起来。但是,我正在考虑将 Flask 作为同时为多个客户端提供服务的独立服务器运行。
Is this possible? Do I have to handle spawning multiple threads and managing them?
这可能吗?我是否必须处理生成多个线程并管理它们?
采纳答案by Sean Vieira
flask.Flask.runaccepts additional keyword arguments (**options) that it forwards to werkzeug.serving.run_simple- two of those arguments are threaded(a boolean) and processes(which you can set to a number greater than one to have werkzeug spawn more than one process to handle requests).
flask.Flask.run接受**options它转发到的其他关键字参数 ( ) werkzeug.serving.run_simple- 其中两个参数是threaded(布尔值)和processes(您可以将其设置为大于 1 的数字,以使 werkzeug 产生多个进程来处理请求)。
threadeddefaults to Trueas of Flask 1.0, so for the latest versions of Flask, the default development server will be able to serve multiple clients simultaneously by default. For older versions of Flask, you can explicitly pass threaded=Trueto enable this behaviour.
threaded默认True从 Flask 1.0 开始,所以对于最新版本的 Flask,默认开发服务器默认能够同时为多个客户端提供服务。对于旧版本的 Flask,您可以显式传递threaded=True以启用此行为。
For example, you can do
例如,你可以做
if __name__ == '__main__':
app.run(threaded=True)
to handle multiple clients using threads in a way compatible with old Flask versions, or
以与旧 Flask 版本兼容的方式使用线程处理多个客户端,或
if __name__ == '__main__':
app.run(threaded=False, processes=3)
to tell Werkzeug to spawn three processes to handle incoming requests, or just
告诉 Werkzeug 产生三个进程来处理传入的请求,或者只是
if __name__ == '__main__':
app.run()
to handle multiple clients using threads if you know that you will be using Flask 1.0 or later.
如果您知道将使用 Flask 1.0 或更高版本,则使用线程处理多个客户端。
That being said, Werkzeug's serving.run_simplewraps the standard library's wsgirefpackage - and that package contains a reference implementation of WSGI, not a production-ready web server. If you are going to use Flask in production (assuming that "production" is not a low-traffic internal application with no more than 10 concurrent users) make sure to stand it up behind a real web server (see the section of Flask's docs entitled Deployment Optionsfor some suggested methods).
话虽如此,Werkzeugserving.run_simple包装了标准库的wsgiref包 - 该包包含 WSGI 的参考实现,而不是生产就绪的 Web 服务器。如果您打算在生产中使用 Flask(假设“生产”不是并发用户不超过 10 个的低流量内部应用程序),请确保将其置于真正的 Web 服务器后面(请参阅 Flask 文档中标题为一些建议方法的部署选项)。
回答by Ryan Artecona
Using the simple app.run()from within Flask creates a single synchronous server on a single thread capable of serving only one client at a time. It is intended for use in controlled environments with low demand (i.e. development, debugging) for exactly this reason.
app.run()在 Flask 中使用 simple from 在单个线程上创建一个同步服务器,一次只能为一个客户端提供服务。正是出于这个原因,它旨在用于低需求(即开发、调试)的受控环境中。
Spawning threads and managing them yourself is probably not going to get you very far either, because of the Python GIL.
由于Python GIL,生成线程并自己管理它们也可能不会让您走得很远。
That said, you do still have some good options. Gunicornis a solid, easy-to-use WSGI server that will let you spawn multiple workers (separate processes, so no GIL worries), and even comes with asynchronous workersthat will speed up your app (and make it more secure) with little to no work on your part (especially with Flask).
也就是说,您仍然有一些不错的选择。Gunicorn是一个可靠、易于使用的 WSGI 服务器,它可以让你产生多个工作程序(单独的进程,所以不用担心 GIL),甚至带有异步工作程序,可以加速你的应用程序(并使其更安全)对您没有任何工作(尤其是使用 Flask)。
Still, even Gunicorn should probably not be directly publicly exposed. In production, it should be used behind a more robust HTTP server; nginxtends to go well with Gunicorn and Flask.
尽管如此,即使是 Gunicorn 也不应该直接公开曝光。在生产中,它应该在更强大的 HTTP 服务器后面使用;nginx往往与 Gunicorn 和 Flask 搭配得很好。

