From 4225f1525fa17161954abbdc97c12af8bb04a0e0 Mon Sep 17 00:00:00 2001 From: 3030332422 <3030332422@qq.com> Date: Tue, 14 Apr 2026 17:59:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=20RuntimeInstanceLock?= =?UTF-8?q?=20=E9=94=81=E6=96=87=E4=BB=B6=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaozhi-server/test/start_test_runtime.py | 51 ------------------- 1 file changed, 51 deletions(-) diff --git a/main/xiaozhi-server/test/start_test_runtime.py b/main/xiaozhi-server/test/start_test_runtime.py index 5a0fdf72..a4b9facf 100644 --- a/main/xiaozhi-server/test/start_test_runtime.py +++ b/main/xiaozhi-server/test/start_test_runtime.py @@ -1,63 +1,13 @@ -import os import threading from pathlib import Path -if os.name == "nt": - import msvcrt -else: - import fcntl - from wakeword_runtime.config import load_config, setup_logging from wakeword_runtime.runtime import TestRuntimeApplication, TestRuntimeHttpServer -class RuntimeInstanceLock: - def __init__(self, lock_path: Path) -> None: - self.lock_path = lock_path - self._handle = None - - def acquire(self) -> bool: - self.lock_path.parent.mkdir(parents=True, exist_ok=True) - self._handle = open(self.lock_path, "a+b") - try: - if os.name == "nt": - self._handle.seek(0) - msvcrt.locking(self._handle.fileno(), msvcrt.LK_NBLCK, 1) - else: - fcntl.flock(self._handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) - self._handle.seek(0) - self._handle.truncate() - self._handle.write(str(os.getpid()).encode("ascii")) - self._handle.flush() - return True - except OSError: - self.release() - return False - - def release(self) -> None: - if self._handle is None: - return - try: - if os.name == "nt": - self._handle.seek(0) - msvcrt.locking(self._handle.fileno(), msvcrt.LK_UNLCK, 1) - else: - fcntl.flock(self._handle.fileno(), fcntl.LOCK_UN) - except OSError: - pass - finally: - self._handle.close() - self._handle = None - - def main() -> int: test_root = Path(__file__).resolve().parent runtime_root = test_root / "wakeword_runtime" - lock = RuntimeInstanceLock(runtime_root / ".runtime.lock") - if not lock.acquire(): - print("failed to start test runtime: another test runtime instance is already running") - print("请先关闭已有的 test runtime 进程,再重新启动。") - return 1 config = load_config(runtime_root) setup_logging(config.log_file, config.log_level) @@ -94,7 +44,6 @@ def main() -> int: with app_lock: app.shutdown() http_server.shutdown() - lock.release() return 0