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

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

问题描述

我知道我可以将Flask与Apache或其他Web服务器连接起来。但是,我正在考虑将Flask作为独立服务器同时运行多个客户端。



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

解决方案

flask.Flask.run 接受额外的关键字参数( ** options ),它转发到 werkzeug.serving.run_simple - 其中两个参数是线程(您可以设置为 True >启用线程)和 processes (你可以设置一个大于1的数字,让werkzeug产生多个进程来处理请求)。所以,如果你这样做:

$ p $ if __name__ =='__main__':
app.run(threaded = True)
#或者
#app.run(processes = 3)

Werkzeug使用线程,并产生三个进程来处理传入的请求。



这就是说,Werkzeug的 serving.run_simple 封装标准库的 wsgiref 软件包 - 包中包含WSGI的参考实现,而不是生产就绪的Web服务器。如果您打算在生产环境中使用Flask(假设生产不是低流量的内部应用程序,并发用户不超过10个),请确保将其置于真正的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 accepts additional keyword arguments (**options) that it forwards to werkzeug.serving.run_simple - two of those arguments are threaded (which you can set to True to enable threading) and processes (which you can set to a number greater than one to have werkzeug spawn more than one process to handle requests). So if you do:

if __name__ == '__main__':
    app.run(threaded=True)
    # Alternately
    # app.run(processes=3)

Flask will tell Werkzeug to use threading and to spawn three processes to handle incoming requests.

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天全站免登陆