對於flask應用
啟動命令為 python app.py
使用gunicorn啟動
pip install gunicorn
python gunicorn --workers=7 switch_app:app -b 0.0.0.0:6002
將gunicorn的配置參數寫入文件 config.py
python gunicorn -c config.py switch_app:app
其中switch_app為文件名 switch_app.py ,app為文件中的app對象
config.py的代碼如下
import os
import gevent.monkey
gevent.monkey.patch_all()
import multiprocessing
#debug = True
loglevel = 'debug'
bind = "0.0.0.0:6002"
pidfile = "logs/gunicorn.pid"
accesslog = "logs/access.log"
errorlog = "logs/debug.log"
daemon = True
timeout = 180
#啟動進程數
workers=multiprocessing.cpu_count()
worker_class = 'gevent'
x_forwarded_for_header = 'X-FORWARDED-FOR'
其中timeout=180表示超過180秒未反應就關閉該請求響應,會得到請求被異常關閉的日志信息,默認超時時間為60秒左右