Python 使用 threaded=True 并发处理 Flask 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38876721/
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
Handle Flask requests concurrently with threaded=True
提问by Harrison
What exactly does passing threaded = True
to app.run()
do?
究竟是通过threaded = True
对app.run()
吗?
My application processes input from the user, and takes a bit of time to do so. During this time, the application is unable to handle other requests. I have tested my application with threaded=True
and it allows me to handle multiple requests concurrently.
我的应用程序处理来自用户的输入,并需要一些时间来完成。在此期间,应用程序无法处理其他请求。我已经测试了我的应用程序,threaded=True
它允许我同时处理多个请求。
回答by Martijn Pieters
As of Flask 1.0, the WSGI server included with Flask is run in threaded mode by default.
从 Flask 1.0 开始,Flask 中包含的 WSGI 服务器默认以线程模式运行。
Prior to 1.0, or if you disable threading, the server is run in single-threaded mode, and can only handle one request at a time. Any parallel requests will have to wait until they can be handled, which can lead to issues if you tried to contact your own server from a request.
在 1.0 之前,或者如果禁用线程,则服务器以单线程模式运行,并且一次只能处理一个请求。任何并行请求都必须等待,直到它们可以被处理,如果您尝试通过请求联系您自己的服务器,这可能会导致问题。
With threaded=True
requests are each handled in a new thread. How many threads your server can handle concurrently depends entirely on your OS and what limits it sets on the number of threads per process. The implementation uses the SocketServer.ThreadingMixIn
class, which sets no limits to the number of threads it can spin up.
随着threaded=True
请求每一个新的线程来处理。您的服务器可以同时处理多少线程完全取决于您的操作系统以及它对每个进程的线程数设置的限制。该实现使用SocketServer.ThreadingMixIn
class,它对它可以启动的线程数没有限制。
Note that the Flask server is designed for development only. It is nota production-ready server. Don't rely on it to run your site on the wider web. Use a proper WSGI server (like gunicornor uWSGI) instead.
请注意,Flask 服务器仅用于开发。它不是生产就绪的服务器。不要依赖它在更广泛的网络上运行您的网站。改用合适的 WSGI 服务器(如gunicorn或uWSGI)。
回答by Paul Becotte
How many requests will my application be able to handle concurrently with this statement?
我的应用程序能够与此语句同时处理多少个请求?
This depends drastically on your application. Each new request will have a thread launched- it depends on how many threads your machine can handle. I don't see an option to limit the number of threads (like uwsgi offers in a production deployment).
这在很大程度上取决于您的应用程序。每个新请求都会启动一个线程 - 这取决于您的机器可以处理多少线程。我没有看到限制线程数的选项(如生产部署中的 uwsgi 提供)。
What are the downsides to using this? If i'm not expecting more than a few requests concurrently, can I just continue to use this?
使用它有什么缺点?如果我不希望同时收到多个请求,我可以继续使用它吗?
Switching from a single thread to multi-threaded can lead to concurrency bugs... if you use this be careful about how you handle global objects (see the g object in the documentation!) and state.
从单线程切换到多线程可能会导致并发错误……如果您使用它,请注意如何处理全局对象(请参阅文档中的 g 对象!)和状态。