gunicorn_config配置文件
代码:
from imp import reload import os from distutils.util import strtobool BASE_PATH = os.path.abspath(os.path.dirname(__file__)) # 指定应用 wsgi_app ='app:app' # 是否让进入后台运行, docker内肯定设置False, 因为必须有一直前台运行的进程 daemon = False #################debugging############ reload = False #################logging############ # 设置进程文件目录 # pidfile = '/var/run/gunicorn.pid' pidfile = os.path.join(BASE_PATH, 'gunicorn.pid') # 设置访问日志输出文件地址, -表示输出到标准输出 accesslog = '-' # 禁用将访问日志转发到syslog disable_redirect_access_to_syslog = True # 日志访问格式 access_log_format = '%(h)s %(l)s %(t)s %(p)s "%(r)s" %(U)s %(s)s %(l)s %(b)s %(l)s %(L)s' # 设置错误日志访问地址, -表示输出到标准错误 errorlog = '-' # 设置日志记录水平 loglevel = 'info' # 是否重定向标准输出和标准错误到errorlog去, 默认是False # capture_output = False # 处理日志的日志类,下面是default的值 # logger_class = 'gunicorn.glogging.Logger' # 配置logger的设置, 是不是这里可以设置日志旋转 # logconfig = None # 通过字典配置logger,优先级高于logconfig # logconfig_dict = {} # 系统日志syslog地址 # syslog_addr = 'udp://localhost:514' # 是否启动将gunicorn日志发送到syslog,默认False # syslog = False # statsd是一款数据采集程序, StatsD系统包括三部分:客户端(client)、服务器(server)和后端(backend) # 如果设置了,就是把一些数据上送上去, 后面好分析 # statsd_host = None # 也是关于statsd的设置 # dogstatsd_tags = '' # 发出statsd度量时使用的前缀 # statsd_prefix = '' #################server socket############ # 监听内网端口 bind = '0.0.0.0:8888' # 最大等待连接的客户端数量 backlog = 2048 #################worker processes############ # worker数量. 实际就是进程数量 if os.environ.get('ENV', 'local') == 'local': workers = int(os.environ.get('WORKERS', 2)) else: workers = int(os.environ.get('WORKERS', 8)) # 工作模式协程 # worker_class = 'sync' worker_class = 'gevent' # 设置线程数量,这个参数只在worker类型是Gthread时有效 # 如果使用了sync类型worker,又设置threads大于1,则会使用gthread替代sync # threads = 1 # 最大并发客户端数量, 这个设置仅影响Eventlet and Gevent worker类型 worker_connections = 10000 # 一个worker在处理一定数量的request后可以让他自动重启, 这是一种帮助限制内存泄漏损害的简单方法. 默认是0, 禁用这个功能 # max_requests = 0 # 这是为了错开worker重启以避免所有worker同时重启,导致服务不可用 max_requests_jitter = 2 # gunicorn 给子进程的执行时间,如果超过这个限制就会被父进程kill掉,然后重启一个新的。 # 比如处理一个请求30秒内worker不能进行返回结果,gunicorn就认定这个request超时,终止worker继续执行,向客户端返回出错的信息, # 用于避免系统出现异常时,所有worker都被占住,无法处理更多的正常request,导致整个系统无法访问 # 所以可以检查一下为什么这个request会耗那么久的时间(超过30秒),如果是正常的话,可以适当调高gunicorn的超时限制或者使用异步的worker, # 如果是系统处理速度遇到瓶颈,那可能要从数据库,缓存,算法等各个方面来提升速度。 timeout = 200 #这个参数只对woker是sync方式起作用 # 优雅关闭,指接收到重启信号, 工作进程不是立马被杀死重启,而是等待一定时间处理可能正在处理的任务,然后才重启 # graceful_timeout = 150 keepalive = 60 #################server Hooks############ # def on_starting(server): # ''' # 这个是主进程启动时会被调用 # ''' # pass # def on_reload(server): # ''' # Called to recycle workers during a reload via SIGHUP. # ''' # pass # def when_ready(sever): # ''' # 当gunicorn准备就绪, 坐等请求过来时调用 # ''' # pass # def pre_fork(sever, worker): # ''' # 创建子进程之前调用, 有几个子进程就应该被调用几次 # ''' # pass # def post_fork(sever, worker): # ''' # 子进程创建成功了被调用,有几个成功就被调用几次 # ''' # pass # def post_worker_init(worker): # ''' # worker进程初始化完成application # ''' # pass # def worker_int(worker): # ''' # 接收到SIGINT or SIGQUIT信号时,工作进程退出调用 # ''' # pass # def worker_abort(worker): # ''' # 接收到SIGABRT信号时被调用,通常发生timeout时,处理请求耗费时间超过了设置的timeout值, # 这里应该主动上报错误,定位为什么处理那么长时间 # ''' # pass # def pre_exec(server): # ''' # 一个新的master进程forked时调用 # ''' # pass # def pre_request(worker, req): # ''' # 在worker开始处理请求前调用 # ''' # pass # def post_request(worker, req, environ, resp): # ''' # 处理完成请求时调用 # ''' # pass # def child_exit(sever, worker): # ''' # Called just after a worker has been exited, in the master process. # ''' # pass # def worker_exit(server, worker): # ''' # 工作进程退出时调用 # ''' # pass # def nworkers_changed(server, new_value, old_value): # ''' # workers的数量发生改变时被调用 # ''' # pass # def on_exit(server): # ''' # gunicorn退出时调用 # ''' # pass
gunicorn -h usage: gunicorn [OPTIONS] [APP_MODULE] optional arguments: -h, --help show this help message and exit -v, --version show program's version number and exit -c CONFIG, --config CONFIG The Gunicorn config file. [None] -b ADDRESS, --bind ADDRESS The socket to bind. [['127.0.0.1:8000']] --backlog INT The maximum number of pending connections. [2048] -w INT, --workers INT The number of worker processes for handling requests. [1] -k STRING, --worker-class STRING The type of workers to use. [sync] --threads INT The number of worker threads for handling requests. [1] --worker-connections INT The maximum number of simultaneous clients. [1000] --max-requests INT The maximum number of requests a worker will process before restarting. [0] --max-requests-jitter INT The maximum jitter to add to the *max_requests* setting. [0] -t INT, --timeout INT Workers silent for more than this many seconds are killed and restarted. [30] --graceful-timeout INT Timeout for graceful workers restart. [30] --keep-alive INT The number of seconds to wait for requests on a Keep- Alive connection. [2] --limit-request-line INT The maximum size of HTTP request line in bytes. [4094] --limit-request-fields INT Limit the number of HTTP headers fields in a request. [100] --limit-request-field_size INT Limit the allowed size of an HTTP request header field. [8190] --reload Restart workers when code changes. [False] --reload-engine STRING The implementation that should be used to power :ref:`reload`. [auto] --reload-extra-file FILES Extends :ref:`reload` option to also watch and reload on additional files [[]] --spew Install a trace function that spews every line executed by the server. [False] --check-config Check the configuration. [False] --preload Load application code before the worker processes are forked. [False] --no-sendfile Disables the use of ``sendfile()``. [None] --reuse-port Set the ``SO_REUSEPORT`` flag on the listening socket. [False] --chdir CHDIR Chdir to specified directory before apps loading. [/data/www/xyz123] -D, --daemon Daemonize the Gunicorn process. [False] -e ENV, --env ENV Set environment variable (key=value). [[]] -p FILE, --pid FILE A filename to use for the PID file. [None] --worker-tmp-dir DIR A directory to use for the worker heartbeat temporary file. [None] -u USER, --user USER Switch worker processes to run as this user. [0] -g GROUP, --group GROUP Switch worker process to run as this group. [0] -m INT, --umask INT A bit mask for the file mode on files written by Gunicorn. [0] --initgroups If true, set the worker process's group access list with all of the [False] --forwarded-allow-ips STRING Front-end's IPs from which allowed to handle set secure headers. [127.0.0.1] --access-logfile FILE The Access log file to write to. [None] --disable-redirect-access-to-syslog Disable redirect access logs to syslog. [False] --access-logformat STRING The access log format. [%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"] --error-logfile FILE, --log-file FILE The Error log file to write to. [-] --log-level LEVEL The granularity of Error log outputs. [info] --capture-output Redirect stdout/stderr to specified file in :ref:`errorlog`. [False] --logger-class STRING The logger you want to use to log events in Gunicorn. [gunicorn.glogging.Logger] --log-config FILE The log config file to use. [None] --log-config-dict LOGCONFIG_DICT The log config dictionary to use, using the standard Python [{}] --log-syslog-to SYSLOG_ADDR Address to send syslog messages. [udp://localhost:514] --log-syslog Send *Gunicorn* logs to syslog. [False] --log-syslog-prefix SYSLOG_PREFIX Makes Gunicorn use the parameter as program-name in the syslog entries. [None] --log-syslog-facility SYSLOG_FACILITY Syslog facility name [user] -R, --enable-stdio-inheritance Enable stdio inheritance. [False] --statsd-host STATSD_ADDR ``host:port`` of the statsd server to log to. [None] --statsd-prefix STATSD_PREFIX Prefix to use when emitting statsd metrics (a trailing ``.`` is added, [] -n STRING, --name STRING A base to use with setproctitle for process naming. [None] --pythonpath STRING A comma-separated list of directories to add to the Python path. [None] --paste STRING, --paster STRING Load a PasteDeploy config file. The argument may contain a ``#`` [None] --proxy-protocol Enable detect PROXY protocol (PROXY mode). [False] --proxy-allow-from PROXY_ALLOW_IPS Front-end's IPs from which allowed accept proxy requests (comma separate). [127.0.0.1] --keyfile FILE SSL key file [None] --certfile FILE SSL certificate file [None] --ssl-version SSL_VERSION SSL version to use (see stdlib ssl module's) [2] --cert-reqs CERT_REQS Whether client certificate is required (see stdlib ssl module's) [0] --ca-certs FILE CA certificates file [None] --suppress-ragged-eofs Suppress ragged EOFs (see stdlib ssl module's) [True] --do-handshake-on-connect Whether to perform SSL handshake on socket connect (see stdlib ssl module's) [False] --ciphers CIPHERS Ciphers to use (see stdlib ssl module's) [TLSv1] --paste-global CONF Set a PasteDeploy global config variable in ``key=value`` form. [[]]
配置项和-h输出是对应的
参考:
https://blog.csdn.net/weixin_48232453/article/details/141606371
https://blog.csdn.net/y472360651/article/details/78538188
https://docs.gunicorn.org/en/stable/settings.html#server-mechanics
本文来自博客园,作者:河北大学-徐小波,转载请注明原文链接:https://www.cnblogs.com/xuxiaobo/p/18727309

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步