feat: add Gunicorn preload option and initialize shared resources
parent
d9b7810cc8
commit
1af14bdb27
|
|
@ -18,6 +18,7 @@ class GunicornService(BaseService):
|
||||||
bind = f'{HTTP_HOST}:{HTTP_PORT}'
|
bind = f'{HTTP_HOST}:{HTTP_PORT}'
|
||||||
cmd = [
|
cmd = [
|
||||||
'gunicorn', 'maxkb.wsgi:application',
|
'gunicorn', 'maxkb.wsgi:application',
|
||||||
|
'--preload',
|
||||||
'-b', bind,
|
'-b', bind,
|
||||||
'-k', 'gthread',
|
'-k', 'gthread',
|
||||||
'--threads', '200',
|
'--threads', '200',
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ class GunicornLocalModelService(BaseService):
|
||||||
worker = CONFIG.get("LOCAL_MODEL_HOST_WORKER", 1)
|
worker = CONFIG.get("LOCAL_MODEL_HOST_WORKER", 1)
|
||||||
cmd = [
|
cmd = [
|
||||||
'gunicorn', 'maxkb.wsgi:application',
|
'gunicorn', 'maxkb.wsgi:application',
|
||||||
|
'--preload',
|
||||||
'-b', bind,
|
'-b', bind,
|
||||||
'-k', 'gthread',
|
'-k', 'gthread',
|
||||||
'--threads', '200',
|
'--threads', '200',
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ class SchedulerService(BaseService):
|
||||||
bind = f'127.0.0.1:6060'
|
bind = f'127.0.0.1:6060'
|
||||||
cmd = [
|
cmd = [
|
||||||
'gunicorn', 'maxkb.wsgi:application',
|
'gunicorn', 'maxkb.wsgi:application',
|
||||||
|
'--preload',
|
||||||
'-b', bind,
|
'-b', bind,
|
||||||
'-k', 'gthread',
|
'-k', 'gthread',
|
||||||
'--threads', '200',
|
'--threads', '200',
|
||||||
|
|
|
||||||
|
|
@ -10,29 +10,57 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from django.core.wsgi import get_wsgi_application
|
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')
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'maxkb.settings')
|
||||||
|
|
||||||
application = get_wsgi_application()
|
application = get_wsgi_application()
|
||||||
|
|
||||||
|
|
||||||
|
# -----------------------------
|
||||||
def post_handler():
|
# 全局初始化,只希望在 master preload 阶段执行一次
|
||||||
|
# -----------------------------
|
||||||
|
def preloaded_init():
|
||||||
from common.database_model_manage.database_model_manage import DatabaseModelManage
|
from common.database_model_manage.database_model_manage import DatabaseModelManage
|
||||||
from common import event
|
from common import event
|
||||||
|
from common.utils.logger import maxkb_logger
|
||||||
|
|
||||||
event.run()
|
event.run()
|
||||||
DatabaseModelManage.init()
|
DatabaseModelManage.init()
|
||||||
|
|
||||||
|
if os.environ.get("ENABLE_SCHEDULER") == "1":
|
||||||
|
from common import job
|
||||||
|
|
||||||
def post_scheduler_handler():
|
job.run()
|
||||||
from common import job
|
|
||||||
|
|
||||||
job.run()
|
maxkb_logger.info("✅ preloaded_init: master 初始化完成,内存将被 worker 共享")
|
||||||
|
|
||||||
# 启动后处理函数
|
|
||||||
post_handler()
|
|
||||||
|
|
||||||
# 仅在scheduler中启动定时任务,dev local_model celery 不需要
|
# Gunicorn preload 阶段会执行此逻辑
|
||||||
if os.environ.get('ENABLE_SCHEDULER') == '1':
|
if os.environ.get("GUNICORN_PRELOAD", "1") == "1":
|
||||||
post_scheduler_handler()
|
try:
|
||||||
|
preloaded_init()
|
||||||
|
except Exception as e:
|
||||||
|
import traceback
|
||||||
|
from common.utils.logger import maxkb_logger
|
||||||
|
|
||||||
|
maxkb_logger.info("⚠️ preload 初始化失败:", e)
|
||||||
|
traceback.print_exc()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue