diff --git a/operations/screenshot_service.py b/operations/screenshot_service.py index b0c3272..264154a 100644 --- a/operations/screenshot_service.py +++ b/operations/screenshot_service.py @@ -8,6 +8,7 @@ from __future__ import annotations +import atexit import os import threading import time @@ -336,3 +337,15 @@ def get_optimal_workers() -> int: if cpu_count >= 2: return 3 return 2 + + +def _shutdown_screenshot_service() -> None: + """atexit 注册的清理函数,确保进程退出时优雅关闭截图服务""" + try: + if ScreenshotService._instance is not None: + ScreenshotService._instance.stop() + except Exception: + pass + + +atexit.register(_shutdown_screenshot_service)