Files
syops/backend/worker_kb.py
T

54 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
backend/worker_kb.py
用途:知识库 worker 部署兼容入口(Compose / Supervisor 沿用此路径)。
逻辑:Wave 2 转调 worker.py,默认 --queues kb;保留多进程并发配置。
"""
from __future__ import annotations
import multiprocessing as mp
import sys
from app import create_app
from worker import _configure_logging, _resolve_config_name, main
def _worker_loop(worker_index: int) -> None:
log = _configure_logging(str(worker_index))
log.info('kb-worker process started index=%s', worker_index)
main(queues=['kb'], config_name=_resolve_config_name())
def main_kb() -> None:
"""启动 KB worker;KB_WORKER_CONCURRENCY>1 时 fork 多进程。"""
bootstrap_app = create_app(_resolve_config_name())
concurrency = max(int(bootstrap_app.config.get('KB_WORKER_CONCURRENCY', 1) or 1), 1)
if concurrency == 1:
main(queues=['kb'], config_name=_resolve_config_name())
return
log = _configure_logging('master')
log.info('starting %s kb 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 kb 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_kb()