diff --git a/apps/common/management/commands/services/services/gunicorn.py b/apps/common/management/commands/services/services/gunicorn.py index 331d1d3ea..788c98560 100644 --- a/apps/common/management/commands/services/services/gunicorn.py +++ b/apps/common/management/commands/services/services/gunicorn.py @@ -18,6 +18,7 @@ class GunicornService(BaseService): bind = f'{HTTP_HOST}:{HTTP_PORT}' cmd = [ 'gunicorn', 'maxkb.wsgi:application', + '--preload', '-b', bind, '-k', 'gthread', '--threads', '200', diff --git a/apps/common/management/commands/services/services/local_model.py b/apps/common/management/commands/services/services/local_model.py index 35c5ae92e..4fea383fd 100644 --- a/apps/common/management/commands/services/services/local_model.py +++ b/apps/common/management/commands/services/services/local_model.py @@ -28,6 +28,7 @@ class GunicornLocalModelService(BaseService): worker = CONFIG.get("LOCAL_MODEL_HOST_WORKER", 1) cmd = [ 'gunicorn', 'maxkb.wsgi:application', + '--preload', '-b', bind, '-k', 'gthread', '--threads', '200', diff --git a/apps/common/management/commands/services/services/scheduler.py b/apps/common/management/commands/services/services/scheduler.py index e9a0bd97a..e9ef67535 100644 --- a/apps/common/management/commands/services/services/scheduler.py +++ b/apps/common/management/commands/services/services/scheduler.py @@ -20,6 +20,7 @@ class SchedulerService(BaseService): bind = f'127.0.0.1:6060' cmd = [ 'gunicorn', 'maxkb.wsgi:application', + '--preload', '-b', bind, '-k', 'gthread', '--threads', '200', diff --git a/apps/maxkb/wsgi.py b/apps/maxkb/wsgi.py index fc271a7c3..7b9e60018 100644 --- a/apps/maxkb/wsgi.py +++ b/apps/maxkb/wsgi.py @@ -10,29 +10,57 @@ 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() - -def post_handler(): +# ----------------------------- +# 全局初始化,只希望在 master preload 阶段执行一次 +# ----------------------------- +def preloaded_init(): 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 -def post_scheduler_handler(): - from common import job + job.run() - job.run() + maxkb_logger.info("✅ preloaded_init: master 初始化完成,内存将被 worker 共享") -# 启动后处理函数 -post_handler() -# 仅在scheduler中启动定时任务,dev local_model celery 不需要 -if os.environ.get('ENABLE_SCHEDULER') == '1': - post_scheduler_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()