方式一:命令行
gunicorn pl_uwsgi:app -w 4 -t 60 -preload -b 0.0.0.0:8501 -k gevent
加上参数 -preload,可以看到详细的报错信息
详细参数配置:
http://docs.gunicorn.org/en/stable/settings.html#server-mechanics
方式二:配置文件
新建gunicorn_config.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# date 20210426
# 监听本机的端口
bind = "0.0.0.0:8501"
# 未决连接的最大数量,即等待服务的客户的数量
backlog = 2048
# 进程数
workers = 8
# 线程数
threads=2
# 工作模式为gevent
worker_class = 'gevent'
# 最大客户端并发数量,默认情况下这个值为1000。
worker_connections = 1000
# 超时 默认30秒
timeout = 120
# 连接上等待请求的秒数,默认情况下值为2
keepalive = 2
# 根目录
chdir = '/opt/pymodel/project_lyj_model/SmartXiaoLe/ai_platform/'
# 日志格式
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'loggers': {
"gunicorn.error": {
"level": "DEBUG", # 打日志的等级可以换的,下面的同理
"handlers": ["error_file"], # 对应下面的键
"propagate": 1,
"qualname": "gunicorn.error"
},
"gunicorn.access": {
"level": "DEBUG",
"handlers": ["access_file"],
"propagate": 0,
"qualname": "gunicorn.access"
}
},
'handlers': {
"error_file": {
"class": "logging.handlers.RotatingFileHandler",
"maxBytes": 1024 * 1024 * 1024, # 打日志的大小,我这种写法是1个G
"backupCount": 1, # 备份多少份,经过测试,最少也要写1,不然控制不住大小
"formatter": "generic", # 对应下面的键
# 'mode': 'w+',
"filename": "error.log" # 打日志的路径
},
"access_file": {
"class": "logging.handlers.RotatingFileHandler",
"maxBytes": 1024 * 1024 * 1024,
"backupCount": 1,
"formatter": "generic",
"filename": "access.log",
}
},
'formatters': {
"generic": {
"format": "'[%(process)d] [%(asctime)s] %(levelname)s [%(filename)s:%(lineno)s] %(message)s'", # 打日志的格式
"datefmt": "[%Y-%m-%d %H:%M:%S %z]", # 时间显示方法
"class": "logging.Formatter"
},
"access": {
"format": "'[%(process)d] [%(asctime)s] %(levelname)s [%(filename)s:%(lineno)s] %(message)s'",
"class": "logging.Formatter"
}
}
}
gunicorn -c gunicorn_config.py app:app