Gunicorn 简单使用
- 配置文件如下
import multiprocessing
bind = '0.0.0.0:8000'
workers = multiprocessing.cpu_count() * 2 + 1
# 服务连接已满时,可容纳的等待连接数,当超过该值时,服务会直接被拒绝
backlog = 2048
worker_class = "gevent"
# 最大连接数
worker_connections = 1000
threads = 30
# 后台运行
daemon = True
proc_name = 'gunicorn_demo'
# 日志输出
access_log = "./logs/access_guni.log"
error_log = "./logs/error/guni.log"
# pid 用于启停
pidfile = "./logs/guni.pid"
timeout = 600
- 服务启停
- 启动
gunicorn -c guni_config.py manage:app
- manage 就是 python 文件
mange.py
- 服务对象叫做 app
- 关闭
kill -9 ./logs/guni.pid
- 重启
kill -HUP ./logs/guni.pid
- 配置 syslog(可输出到 logstash)
syslog = True
syslog_addr = "udp://11.1.1.1:9090"
access_log_format = ''%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"''
- 日志格式解释
- 官方文档
https://docs.gunicorn.org/en/stable/settings.html#access-log-format
变量名 | 释义 | 备注 |
---|
h | remote address | |
l | ‘-’ | |
u | user name | |
t | date of the request | |
r | status line (e.g. GET / HTTP/1.1) | |
m | request method | |
U | URL path without query string | |
q | query string | |
H | protocol | |
s | status | |
B | response length | |
b | response length or ‘-’ (CLF format) | |
f | referer | |
a | user agent | |
T | request time in seconds | |
D | request time in microseconds | |
L | request time in decimal seconds | |
p | process ID | |
{header}i | request header | |
{header}o | response header | |
{variable}e | environment variable | |