From 9ad98fa255ea86b30210a8b5bb4314ee9913bc55 Mon Sep 17 00:00:00 2001 From: XingQue Date: Thu, 9 Jul 2026 01:58:54 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20gunicorn=20=E6=94=B9=E7=94=A8=20gthread?= =?UTF-8?q?=20=E5=B9=B6=E5=85=B3=E9=97=AD=20preload=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E5=B0=8F=E7=A8=8B=E5=BA=8F=E5=B9=B6=E5=8F=91=E6=89=BF?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- gunicorn.conf.py | 46 ++++++++++++++-------------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/gunicorn.conf.py b/gunicorn.conf.py index e9550b9..eeea91f 100644 --- a/gunicorn.conf.py +++ b/gunicorn.conf.py @@ -1,52 +1,34 @@ # /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" -# 工作进程数(根据CPU核心数调整,建议:CPU核心数*2+1) -# 查看CPU核心数:cat /proc/cpuinfo | grep "processor" | wc -l -workers = 4 +# 并发 = workers × threads(gthread 比纯 sync 更抗小程序一进页多接口并发) +cpu_count = multiprocessing.cpu_count() +workers = int(os.environ.get("GUNICORN_WORKERS", max(cpu_count * 2 + 1, 4))) +threads = int(os.environ.get("GUNICORN_THREADS", 2)) +worker_class = "gthread" -# 工作模式(使用gevent支持异步,更高效) -#worker_class = "gevent" -worker_class = "sync" # 改成这个 +max_requests = 2000 +max_requests_jitter = 100 -# 每个worker的最大请求数(防止内存泄漏) -max_requests = 1000 -max_requests_jitter = 50 # 随机抖动,避免所有worker同时重启 +timeout = 60 +graceful_timeout = 30 +keepalive = 5 -# 超时时间(秒) -timeout = 30 -keepalive = 2 +# Django + MySQL 建议关闭 preload,避免 worker 共享陈旧 DB 连接 +preload_app = False -# 预加载应用(减少内存占用,加快请求处理) -preload_app = True - -# 日志配置 -# 访问日志:输出到标准输出,便于OnePanel查看 accesslog = "-" -# 错误日志:输出到标准错误 errorlog = "-" -# 日志级别 loglevel = "info" -# 进程名称(便于识别) proc_name = "a_long_dianjing_gunicorn" - -# 工作进程的用户/组(安全考虑,用非root用户运行) -# user = "www-data" -# group = "www-data" - -# 工作进程临时目录(避免权限问题) worker_tmp_dir = "/dev/shm" -# 限制请求数据大小(防止大文件攻击) limit_request_line = 4094 limit_request_fields = 100 limit_request_field_size = 8190 -# 优雅退出时间 -graceful_timeout = 30 - -# 进程守护(如果用systemd管理,这里设为False) daemon = False \ No newline at end of file