Python 在 Flask 中处理多个请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14672753/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:07:07  来源:igfitidea点击:

Handling multiple requests in Flask

pythonflask

提问by Arno Moonens

My Flask applications has to do quite a large calculation to fetch a certain page. While Flask is doing that function, another user cannot access the website, because Flask is busy with the large calculation.

我的 Flask 应用程序必须进行大量计算才能获取某个页面。当 Flask 正在执行该功能时,另一个用户无法访问该网站,因为 Flask 正忙于进行大量计算。

Is there any way that I can make my Flask application accept requests from multiple users?

有什么方法可以让我的 Flask 应用程序接受来自多个用户的请求?

采纳答案by Martijn Pieters

Yes, deploy your application on a different WSGI server, see the Flask deployment options documentation.

是的,在不同的 WSGI 服务器上部署您的应用程序,请参阅Flask 部署选项文档

The server component that comes with Flask is really only meant for when you are developing your application; even though it can be configured to handle concurrent requests with app.run(threaded=True)(as of Flask 1.0 this is the default). The above document lists several options for servers that can handle concurrent requests andare far more robust and tuneable.

Flask 附带的服务器组件实际上仅用于开发应用程序;即使它可以配置为处理并发请求app.run(threaded=True)(从 Flask 1.0 开始,这是默认设置)。上面的文档列出了可以处理并发请求并且更加健壮和可调整的服务器的几个选项。

回答by LtWorf

For requests that take a long time, you might want to consider starting a background job for them.

对于需要很长时间的请求,您可能需要考虑为它们启动后台作业。