revert: add Gunicorn preload option and initialize shared resources

This reverts commit 1af14bdb27.
This commit is contained in:
CaptainB 2025-11-05 16:39:41 +08:00
parent 7a9c67d5df
commit f417a5aa39
4 changed files with 10 additions and 41 deletions

View File

@ -18,7 +18,6 @@ class GunicornService(BaseService):
bind = f'{HTTP_HOST}:{HTTP_PORT}'
cmd = [
'gunicorn', 'maxkb.wsgi:application',
'--preload',
'-b', bind,
'-k', 'gthread',
'--threads', '200',

View File

@ -28,7 +28,6 @@ class GunicornLocalModelService(BaseService):
worker = CONFIG.get("LOCAL_MODEL_HOST_WORKER", 1)
cmd = [
'gunicorn', 'maxkb.wsgi:application',
'--preload',
'-b', bind,
'-k', 'gthread',
'--threads', '200',

View File

@ -20,7 +20,6 @@ class SchedulerService(BaseService):
bind = f'127.0.0.1:6060'
cmd = [
'gunicorn', 'maxkb.wsgi:application',
'--preload',
'-b', bind,
'-k', 'gthread',
'--threads', '200',

View File

@ -10,57 +10,29 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
import os
from django.core.wsgi import get_wsgi_application
#
# # 检查是否启用 memray 分析
# if os.environ.get('ENABLE_MEMRAY') == '1':
# import memray
# import atexit
#
# # 为每个进程创建单独的追踪文件
# pid = os.getpid()
# output_file = f"memray_output_{pid}.bin"
#
# tracker = memray.Tracker(output_file)
# tracker.__enter__()
#
#
# def cleanup():
# tracker.__exit__(None, None, None)
#
#
# atexit.register(cleanup)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'maxkb.settings')
application = get_wsgi_application()
# -----------------------------
# 全局初始化,只希望在 master preload 阶段执行一次
# -----------------------------
def preloaded_init():
def post_handler():
from common.database_model_manage.database_model_manage import DatabaseModelManage
from common import event
from common.utils.logger import maxkb_logger
event.run()
DatabaseModelManage.init()
if os.environ.get("ENABLE_SCHEDULER") == "1":
from common import job
job.run()
def post_scheduler_handler():
from common import job
maxkb_logger.info("✅ preloaded_init: master 初始化完成,内存将被 worker 共享")
job.run()
# 启动后处理函数
post_handler()
# Gunicorn preload 阶段会执行此逻辑
if os.environ.get("GUNICORN_PRELOAD", "1") == "1":
try:
preloaded_init()
except Exception as e:
import traceback
from common.utils.logger import maxkb_logger
maxkb_logger.info("⚠️ preload 初始化失败:", e)
traceback.print_exc()
# 仅在scheduler中启动定时任务dev local_model celery 不需要
if os.environ.get('ENABLE_SCHEDULER') == '1':
post_scheduler_handler()