服务器Flask+Gunicorn+gevent部署配置项总结
示例配置文件:
# 配置Gunicorn启动后的进程名称,方便top、ps等指令的辨别
proc_name = "test-info"
# 监听本机的5616端口
bind = '0.0.0.0:1080'
# 超时
# timeout = 30
# 工作模式
worker_class = "gevent"
# 设置最大并发量
worker_connections = 2000
# 开启进程
workers = 5
# 每个进程的开启线程
threads = 2
# 如果不使用supervisord之类的进程管理工具可以是进程成为守护进程,否则会出问题
# daemon = False
| 配置项 | 含义 |
|---|---|
| proc_name | 配置 Gunicorn 启动后的进程名称,方便 top、ps 等指令的辨别 |
| bind | 监听本机的 1080 端口 |
| worker_class | 配置工作模式,这里使用的是 gevent |
| worker_connections | 设置最大并发量 |
| workers | 开启的进程数 |
| threads | 每个进程的开启线程数 |
如果不使用类似 supervisord 的进程管理工具,需要将 daemon 配置为 False,否则会出问题。
此代码段的配置可以将 Gunicorn 开启 5 个进程,每个进程开启 2 个线程,监听本机 1080 端口,最大并发量为 2000。如果服务器性能较好,可以适当提高进程数或线程数以提高并发能力。
需要注意的是,如果使用了此项配置,为了有更好的兼容性,需要在程序启动文件的最上方引入 gevent 的 monkey.patch_all() 模块
from gevent import monkey
"""
Without monkey.patch_all()
there would be no benefit from using gevent here
because all the I/O in the application stayed synchronous.
"""
monkey.patch_all()