我可以单独使用 Flask app.run() 为多个客户端提供服务吗? [英] Can I serve multiple clients using just Flask app.run() as standalone?

查看:35
本文介绍了我可以单独使用 Flask app.run() 为多个客户端提供服务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以将 Flask 与 Apache 或其他 Web 服务器链接起来.但是,我想将 Flask 作为同时为多个客户端提供服务的独立服务器运行.

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.

这可能吗?我是否必须处理生成多个线程并管理它们?

Is this possible? Do I have to handle spawning multiple threads and managing them?

推荐答案

flask.Flask.run 接受它转发到 werkzeug.serving.run_simple - 其中两个参数是 threaded(一个布尔值) 和 processes(您可以将其设置为大于 1 的数字,以使 werkzeug 产生多个进程来处理请求).

flask.Flask.run accepts 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).

threaded 从 Flask 1.0 开始默认为 True,所以对于最新版本的 Flask,默认的开发服务器默认可以同时为多个客户端提供服务.对于旧版本的 Flask,您可以显式传递 threaded=True 以启用此行为.

threaded defaults to True as 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=True to enable this behaviour.

例如,你可以这样做

if __name__ == '__main__':
    app.run(threaded=True)

以与旧 Flask 版本兼容的方式使用线程处理多个客户端,或

to handle multiple clients using threads in a way compatible with old Flask versions, or

if __name__ == '__main__':
    app.run(threaded=False, processes=3)

告诉 Werkzeug 产生三个进程来处理传入的请求,或者只是

to tell Werkzeug to spawn three processes to handle incoming requests, or just

if __name__ == '__main__':
    app.run()

如果您知道将使用 Flask 1.0 或更高版本,则使用线程处理多个客户端.

to handle multiple clients using threads if you know that you will be using Flask 1.0 or later.

话虽如此,Werkzeug 的 serving.run_simple 包装了标准库的 wsgiref 包 - 该包包含 WSGI 的参考实现,而不是生产就绪的 Web 服务器.如果您打算在生产中使用 Flask(假设生产"不是并发用户不超过 10 个的低流量内部应用程序),请确保将其置于真正的 Web 服务器后面(请参阅 Flask 文档部分标题为部署选项 一些建议的方法).

That being said, Werkzeug's serving.run_simple wraps the standard library's wsgiref package - 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 Options for some suggested methods).

这篇关于我可以单独使用 Flask app.run() 为多个客户端提供服务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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