2025-06-05 09:56:04 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: MaxKB
|
|
|
|
|
|
@Author:虎
|
|
|
|
|
|
@file: local_model.py
|
|
|
|
|
|
@date:2024/8/21 13:28
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
2025-11-06 02:31:14 +00:00
|
|
|
|
import subprocess
|
|
|
|
|
|
|
2025-06-05 09:56:04 +00:00
|
|
|
|
from maxkb.const import CONFIG
|
|
|
|
|
|
from .base import BaseService
|
|
|
|
|
|
from ..hands import *
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['GunicornLocalModelService']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GunicornLocalModelService(BaseService):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
|
self.worker = kwargs['worker_gunicorn']
|
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def cmd(self):
|
|
|
|
|
|
print("\n- Start Gunicorn Local Model WSGI HTTP Server")
|
|
|
|
|
|
log_format = '%(h)s %(t)s %(L)ss "%(r)s" %(s)s %(b)s '
|
|
|
|
|
|
bind = f'{CONFIG.get("LOCAL_MODEL_HOST")}:{CONFIG.get("LOCAL_MODEL_PORT")}'
|
|
|
|
|
|
worker = CONFIG.get("LOCAL_MODEL_HOST_WORKER", 1)
|
|
|
|
|
|
cmd = [
|
2025-06-13 08:45:47 +00:00
|
|
|
|
'gunicorn', 'maxkb.wsgi:application',
|
2025-06-05 09:56:04 +00:00
|
|
|
|
'-b', bind,
|
|
|
|
|
|
'-k', 'gthread',
|
|
|
|
|
|
'--threads', '200',
|
|
|
|
|
|
'-w', str(worker),
|
|
|
|
|
|
'--max-requests', '10240',
|
|
|
|
|
|
'--max-requests-jitter', '2048',
|
|
|
|
|
|
'--access-logformat', log_format,
|
2025-07-18 02:40:46 +00:00
|
|
|
|
'--access-logfile', '/dev/null',
|
2025-11-04 11:25:01 +00:00
|
|
|
|
'--error-logfile', '-'
|
2025-06-05 09:56:04 +00:00
|
|
|
|
]
|
|
|
|
|
|
if DEBUG:
|
|
|
|
|
|
cmd.append('--reload')
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def cwd(self):
|
|
|
|
|
|
return APPS_DIR
|
2025-11-06 02:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
def open_subprocess(self):
|
|
|
|
|
|
# 复制当前环境变量,并设置 ENABLE_SCHEDULER=1
|
|
|
|
|
|
env = os.environ.copy()
|
|
|
|
|
|
env['SERVER_NAME'] = 'local_model'
|
|
|
|
|
|
kwargs = {
|
|
|
|
|
|
'cwd': self.cwd,
|
|
|
|
|
|
'stderr': self.log_file,
|
|
|
|
|
|
'stdout': self.log_file,
|
|
|
|
|
|
'env': env
|
|
|
|
|
|
}
|
|
|
|
|
|
self._process = subprocess.Popen(self.cmd, **kwargs)
|