From 7d828d3ebf7ffe683912b4d4daedcd376465b6b5 Mon Sep 17 00:00:00 2001 From: dxc Date: Tue, 26 May 2026 12:01:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=98=E6=9B=B4V3.35?= =?UTF-8?q?=E5=B0=86=E5=9B=BE=E5=83=8F=E7=9A=84=E5=A4=84=E7=90=86=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E6=9B=B4=E6=8D=A2=E5=88=B0=E6=96=B0=E8=A1=A8=E5=BD=93?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inventory-backend/app/utils/executor.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 inventory-backend/app/utils/executor.py diff --git a/inventory-backend/app/utils/executor.py b/inventory-backend/app/utils/executor.py new file mode 100644 index 0000000..1145866 --- /dev/null +++ b/inventory-backend/app/utils/executor.py @@ -0,0 +1,25 @@ +import atexit +import logging +from concurrent.futures import ThreadPoolExecutor + +logger = logging.getLogger(__name__) + +# 全局初始化线程池 +_executor = ThreadPoolExecutor(max_workers=4, thread_name_prefix='image_embedding_') + +def run_embedding_task(fn, *args, **kwargs): + """ + 提交后台任务到线程池 + """ + logger.info("Submitting embedding task to background thread...") + return _executor.submit(fn, *args, **kwargs) + +def _shutdown_executor(): + """ + 优雅关闭线程池,在 Gunicorn worker 退出时触发 + """ + logger.info("Shutting down background thread pool...") + _executor.shutdown(wait=False) + +# 注册到系统退出事件,这样就不会报 _shutdown not defined 了 +atexit.register(_shutdown_executor)