perf(kb):知识库数据清洗同步改异步优化;chat中查假期请假agent入口优化
This commit is contained in:
1 parent
61ebb46c0f
commit
c3b63df2f5
11 files changed
+839
-171
No files matched your search
+36
-5
@@ -5,6 +5,7 @@ backend/worker_kb.py
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import multiprocessing as mp
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
@@ -16,12 +17,12 @@ _LOG_FORMAT = '%(asctime)s %(levelname)s [kb-worker] %(message)s'
|
||||
_LOG_DATEFMT = '%Y-%m-%d %H:%M:%S'
|
||||
|
||||
|
||||
def _configure_logging() -> logging.Logger:
|
||||
logger = logging.getLogger('kb-worker')
|
||||
def _configure_logging(worker_label: str = 'main') -> logging.Logger:
|
||||
logger = logging.getLogger(f'kb-worker.{worker_label}')
|
||||
if logger.handlers:
|
||||
return logger
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setFormatter(logging.Formatter(_LOG_FORMAT, datefmt=_LOG_DATEFMT))
|
||||
handler.setFormatter(logging.Formatter(f'{_LOG_FORMAT} [worker={worker_label}]', datefmt=_LOG_DATEFMT))
|
||||
logger.addHandler(handler)
|
||||
logger.setLevel(logging.INFO)
|
||||
logger.propagate = False
|
||||
@@ -58,8 +59,9 @@ def _resolve_config_name() -> str:
|
||||
return mapping.get(env_name, 'production')
|
||||
|
||||
|
||||
def main():
|
||||
log = _configure_logging()
|
||||
def _worker_loop(worker_index: int):
|
||||
worker_label = str(worker_index)
|
||||
log = _configure_logging(worker_label)
|
||||
app = create_app(_resolve_config_name())
|
||||
poll_seconds = float(app.config.get('KB_WORKER_POLL_SECONDS', 2))
|
||||
|
||||
@@ -73,5 +75,34 @@ def main():
|
||||
log.info('processed job: %s', message)
|
||||
|
||||
|
||||
def main():
|
||||
bootstrap_app = create_app(_resolve_config_name())
|
||||
concurrency = max(int(bootstrap_app.config.get('KB_WORKER_CONCURRENCY', 1) or 1), 1)
|
||||
|
||||
if concurrency == 1:
|
||||
_worker_loop(1)
|
||||
return
|
||||
|
||||
log = _configure_logging('master')
|
||||
log.info('starting %s worker processes', concurrency)
|
||||
workers: list[mp.Process] = []
|
||||
for index in range(concurrency):
|
||||
proc = mp.Process(target=_worker_loop, args=(index + 1,), name=f'kb-worker-{index + 1}')
|
||||
proc.start()
|
||||
workers.append(proc)
|
||||
|
||||
try:
|
||||
for proc in workers:
|
||||
proc.join()
|
||||
except KeyboardInterrupt:
|
||||
log.info('stopping worker pool')
|
||||
for proc in workers:
|
||||
if proc.is_alive():
|
||||
proc.terminate()
|
||||
for proc in workers:
|
||||
proc.join(timeout=5)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
mp.freeze_support()
|
||||
main()
|
||||
Reference in new issue
Block a user