Files
syops/backend/worker_kb.py
T
sophie 0503ff76a6 feat(contract): 采购合同对比agent
Signed-off-by: sophie <sophie>

Signed-off-by: sophie <sophie>
2026-07-27 14:47:09 +08:00

56 lines
1.8 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 沿用此路径)。
逻辑:转调 worker.py;第一个进程消费 kb+contract,其余进程仅消费 kb,避免合同 Word 清洗并发争用。
"""
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:
"""启动一个兼容 Worker;仅首进程额外消费合同审核任务。"""
log = _configure_logging(str(worker_index))
log.info('kb-worker process started index=%s', worker_index)
queues = ['kb', 'contract'] if worker_index == 1 else ['kb']
main(queues=queues, 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', 'contract'], 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()