From dfa944b368bf57fba9013cdccd112e2f3788ca42 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 9 Mar 2026 15:04:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20=5Fready=5Fevent?= =?UTF-8?q?=20=E4=BB=A5=E7=A1=AE=E4=BF=9D=E6=96=87=E4=BB=B6=E7=9B=91?= =?UTF-8?q?=E8=A7=86=E5=99=A8=E5=9C=A8=E5=90=AF=E5=8A=A8=E6=97=B6=E5=87=86?= =?UTF-8?q?=E5=A4=87=E5=B0=B1=E7=BB=AA=EF=BC=8C=E4=BC=98=E5=8C=96=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pytests/config_test/test_file_watcher.py | 1 - src/config/file_watcher.py | 13 ++++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pytests/config_test/test_file_watcher.py b/pytests/config_test/test_file_watcher.py index 91dde1d2..a6b86477 100644 --- a/pytests/config_test/test_file_watcher.py +++ b/pytests/config_test/test_file_watcher.py @@ -135,7 +135,6 @@ async def test_add_callback_while_watcher_running(tmp_path: Path): uuid = watcher.subscribe(callback, paths=[file]) await watcher.start() try: - await asyncio.sleep(0.5) # 等待 watcher 建立 baseline with file.open("w") as f: f.write("change") await _wait_for(lambda: calls >= 1) diff --git a/src/config/file_watcher.py b/src/config/file_watcher.py index eadabb96..ee7ac076 100644 --- a/src/config/file_watcher.py +++ b/src/config/file_watcher.py @@ -119,7 +119,9 @@ class FileWatcher: if not self._subscriptions: raise RuntimeError("启动文件监视器前必须至少注册一个订阅") self._running = True + self._ready_event = asyncio.Event() self._task = asyncio.create_task(self._run()) + await self._ready_event.wait() async def stop(self) -> None: if not self._running: @@ -138,9 +140,18 @@ class FileWatcher: async def _run(self) -> None: while self._running: try: - async for changes in awatch(*self._paths, debounce=self._debounce_ms, force_polling=self._force_polling): + async for changes in awatch( + *self._paths, + debounce=self._debounce_ms, + force_polling=self._force_polling, + yield_on_timeout=True, + ): + if not self._ready_event.is_set(): + self._ready_event.set() if not self._running: break + if not changes: + continue normalized_changes = self._normalize_changes(changes) if not normalized_changes: continue