快速使用waitress部署flask项目

572 阅读1分钟

总览

在 Python 中使用 flask 编写 web 后,若直接使用 app.run(),会有以下提示信息:

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.

为了达成更稳定的部署,可以使用 waitress 代替 app.run()

首先 pip install waitress 安装,然后把 app.run() 替换为这样的形式:

from waitress import serve

serve(app, host='127.0.0.1', port=5000)

此时运行就非常干净,没有 WARNING 了。

额外配置

详细见官方文档的 Tuning options 部分。配置项目像 hostport 一样直接传入给 serve() 即可生效。以下列举几个:

选项说明
threads使用的线程数。默认为 4
connection_limit活动的连接通道数上限。默认为 100
cleanup_interval清理通道的最小时间间隔。默认为 30
channel_timeout未收发信息的通道的超时时间。默认为 120

参考来源