gunicorn使用模板
gunicorn使用拢共就两部:1,写好配置,2,启动。
1. gunicorn配置
主要使用两种方式进行配置:1, command line ;2, configuration file。
命令行的参数和配置文件的参数不一样,官方文档有一一对应的说明。https://docs.gunicorn.org/en/stable/settings.html
方式一:command line配置
gunicorn -D -w 4 -b 0.0.0.0:8000 my_service:app
好处是快速启动,缺点是参数一多,命令很长,难以阅读。
gunicorn -h # 查找所有配置参数,不一一赘述。
方式二:configuration file配置
是一个python文件:my_gunicorn.conf.py,使用方式:gunicorn -c my_gunicorn.conf.py app_test:app,app_test是自己的web接口.py文件。
模板如下:
import logging
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing
import gevent.monkey
# 全局变量
gevent.monkey.patch_all() # gevnet猴子补丁模块,实现多任务的方式。
BASE_DIR = os.path.abspath(os.path.dirname(__file__)) # 当前这个配置文件所在的上级文件夹的绝对路径。
chdir = BASE_DIR # gunicorn要切换到的目的工作目录
pidfile = os.path.join(BASE_DIR, './run/gunicorn.pid') # 设置进程文件目录(存储的是master进程的ID)
# 基本设置
debu = True # 当web接口发生变化时,重新加载,方便调试。
bind = '0.0.0.0:5000' # 绑定ip和端口号
timeout = 30