Revert "perf: gunicorn 改用 gthread 并关闭 preload,提升小程序并发承载"

This reverts commit 9ad98fa255.
This commit is contained in:
XingQue
2026-07-09 02:01:55 +08:00
parent 9ad98fa255
commit 07a104b475

View File

@@ -1,34 +1,52 @@
# /opt/1panel/apps/openresty/nginx/www/sites/43.142.166.152.443/django/gunicorn.conf.py # /opt/1panel/apps/openresty/nginx/www/sites/43.142.166.152.443/django/gunicorn.conf.py
import multiprocessing
import os
# 绑定的IP和端口根据你的Nginx配置后端在8001端口
bind = "127.0.0.1:8001" bind = "127.0.0.1:8001"
# 并发 = workers × threadsgthread 比纯 sync 更抗小程序一进页多接口并发 # 工作进程数根据CPU核心数调整建议CPU核心数*2+1
cpu_count = multiprocessing.cpu_count() # 查看CPU核心数cat /proc/cpuinfo | grep "processor" | wc -l
workers = int(os.environ.get("GUNICORN_WORKERS", max(cpu_count * 2 + 1, 4))) workers = 4
threads = int(os.environ.get("GUNICORN_THREADS", 2))
worker_class = "gthread"
max_requests = 2000 # 工作模式使用gevent支持异步更高效
max_requests_jitter = 100 #worker_class = "gevent"
worker_class = "sync" # 改成这个
timeout = 60 # 每个worker的最大请求数防止内存泄漏
graceful_timeout = 30 max_requests = 1000
keepalive = 5 max_requests_jitter = 50 # 随机抖动避免所有worker同时重启
# Django + MySQL 建议关闭 preload避免 worker 共享陈旧 DB 连接 # 超时时间(秒)
preload_app = False timeout = 30
keepalive = 2
# 预加载应用(减少内存占用,加快请求处理)
preload_app = True
# 日志配置
# 访问日志输出到标准输出便于OnePanel查看
accesslog = "-" accesslog = "-"
# 错误日志:输出到标准错误
errorlog = "-" errorlog = "-"
# 日志级别
loglevel = "info" loglevel = "info"
# 进程名称(便于识别)
proc_name = "a_long_dianjing_gunicorn" proc_name = "a_long_dianjing_gunicorn"
# 工作进程的用户/组安全考虑用非root用户运行
# user = "www-data"
# group = "www-data"
# 工作进程临时目录(避免权限问题)
worker_tmp_dir = "/dev/shm" worker_tmp_dir = "/dev/shm"
# 限制请求数据大小(防止大文件攻击)
limit_request_line = 4094 limit_request_line = 4094
limit_request_fields = 100 limit_request_fields = 100
limit_request_field_size = 8190 limit_request_field_size = 8190
# 优雅退出时间
graceful_timeout = 30
# 进程守护如果用systemd管理这里设为False
daemon = False daemon = False