Gunicorn-配置参数

config

参数名参数内容默认
-c , --configconfig_fileNone

配置文件路径,路径形式的字符

使用config参数及配置文件启动,例子:

gunicorn -c gunicorn.conf manager:app

配置文件中的参数

bind

参数名参数内容默认
bindip:port[‘127.0.0.1:8000’]

监听地址和端口

例子:

bind = "%s:%s" % ("0.0.0.0", 9102)

进程相关

workers

参数名参数内容默认
workers进程数量1

用于处理工作进程的数量

worker推荐的数量为当前的CPU个数*2 + 1。

计算当前的CPU个数方法并设置:

import multiprocessing
workers = multiprocessing.cpu_count() * 2

worker_class

参数名参数内容默认
worker_class工作模式sync

多个工作模式:

同步Worker:sync 默认模式,也就是一次只处理一个请求

异步Worker:通过Eventlet、Gevent实现的异步模式

异步IO Worker:目前支持gthread和gaiohttp两种类型

例子:

worker_class = 'gevent'

worker_connections

参数名参数内容默认
worker_connections客户端最大同时连接数1000

使用于gevent和eventlet工作模式

例子:

worker_connections = 5000

threads

参数名参数内容默认
threads线程数1

工作进程中线程的数,建议值2-4 x CPU核心数

此配置只适用于gthread 进程工作方式, 因为gevent这种使用的是协程工作方式。

max_requests

参数名参数内容默认
max_requests最大请求数0

重启之前,worker将处理的最大请求数

0,默认值时,则禁止了worker的自动重启

这个方法主要是防止内存泄露

当超过max_requests时,就会重启worker
在这里插入图片描述
阿里云服务器TCP连接数(Count):
在这里插入图片描述

max_requests_jitter

参数名参数内容默认
max_requests_jittermax_requests的最大抖动值0

抖动将导致每个工作的重启被随机化,

这是为了避免所有工作被重启。randint(0,max-requests-jitter)

timeout

参数名参数内容默认
timeout秒数30秒

worker超时时间,超时重启

graceful_timeout

参数名参数内容默认
graceful_timeout秒数接收到restart信号后,worker可以在graceful_timeout时间内,继续处理完当前requests

keepalive

参数名参数内容默认
keepaliveINT连接上等待请求的秒数,默认情况下值为2。一般设定在1~5秒之间。

安全

limit_request_line

参数名参数内容默认
limit_request_line最大数4094

http request line最大字节数。值范围0-8190, 0表示无限制。

limit_request_fields

参数名参数内容默认
limit_request_fields最大值100

http request中 header字段数的最大值,最大32768

limit_request_field_size

参数名参数内容默认
limit_request_field_size最大值8190

http request header字段最大字节数,0时无限制

调试

reload

参数名参数内容默认
reloadTrue/FalseFalse

当代码有修改时,自动重启workers。适用于开发环境。

reload_extra_files

参数名参数内容默认
reload_extra_filesstr[ ]

扩展reload配置,增加templates,configurations等文件修改监控。

spew

参数名参数内容默认
spewTrue/FalseFalse

check_config

参数名参数内容默认
check_configTrue/FalseFalse

检查配置

server 机制

chdir

参数名参数内容默认
chdir文件路径/home/docs/checkouts/readthedocs.org/user_builds/gunicorn-docs/checkouts/latest/docs/source

在app加载之前,进入到此目录

配合使用效果更佳:

path_of_current_file = os.path.abspath(__file__)
path_of_current_dir = os.path.split(path_of_current_file)[0]
chdir = path_of_current_dir

daemon

参数名参数内容默认
daemonTrue/FalseFalse

应用是否以daemon方式运行。

raw_env

参数名参数内容默认
raw_envkey=value, 传递环境参数。[ ]

pidfile

参数名参数内容默认
pidfile文件路径None

存储pid的文件路径

worker_tmp_dir

参数名参数内容默认
worker_tmp_dir路径None

临时工作目录

user

参数名参数内容默认
user用户名或者用户id1005

指定worker进程的运行用户名

日志

accesslog

参数名参数内容默认
accesslog文件路径None

接收日志文件路径

access_log_format

参数名参数内容默认
access_log_format日志格式%(h)s %(l)s %(u)s %(t)s “%®s” %(s)s %(b)s “%(f)s” “%(a)s”

文档:

https://docs.gunicorn.org/en/latest/settings.html#access-log-format

errorlog

参数名参数内容默认
errorlog文件路径-

错误日志路径

loglever

参数名参数内容默认
logleverstrinfo

日志等级:debug, info, warning, error, critical.

capture_output

重定向stdout/stderr到error log file。

logger_class

日志实现类。缺省gunicorn.glogging.Logger 。

logconfig

日志配置文件。同python标准日志模块logging的配置。

内容整合来自:

https://docs.gunicorn.org/en/latest/settings.html

https://www.jianshu.com/p/69e75fc3e08e

https://blog.csdn.net/y472360651/article/details/78538188

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值