From 783aa26d2339d696e13c4229156886647c53cd36 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Mon, 20 Mar 2023 21:11:51 +0100 Subject: [PATCH] Use watchfiles stop_event --- plugins/contents/fps_contents/fileid.py | 16 ++++++++++------ plugins/yjs/fps_yjs/main.py | 3 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/plugins/contents/fps_contents/fileid.py b/plugins/contents/fps_contents/fileid.py index fbfbe089..89b20a93 100644 --- a/plugins/contents/fps_contents/fileid.py +++ b/plugins/contents/fps_contents/fileid.py @@ -40,6 +40,8 @@ def __init__(self, db_path: str = "fileid.db"): self.initialized = asyncio.Event() self.watchers = {} self.watch_files_task = asyncio.create_task(self.watch_files()) + self.stop_watching_files = asyncio.Event() + self.stopped_watching_files = asyncio.Event() self.lock = asyncio.Lock() async def get_id(self, path: str) -> Optional[str]: @@ -68,11 +70,11 @@ async def index(self, path: str) -> Optional[str]: if not await apath.exists(): return None - idx = uuid4().hex - mtime = (await apath.stat()).st_mtime - await db.execute("INSERT INTO fileids VALUES (?, ?, ?)", (idx, path, mtime)) - await db.commit() - return idx + idx = uuid4().hex + mtime = (await apath.stat()).st_mtime + await db.execute("INSERT INTO fileids VALUES (?, ?, ?)", (idx, path, mtime)) + await db.commit() + return idx async def watch_files(self): async with self.lock: @@ -96,7 +98,7 @@ async def watch_files(self): await db.commit() self.initialized.set() - async for changes in awatch("."): + async for changes in awatch(".", stop_event=self.stop_watching_files): async with self.lock: async with aiosqlite.connect(self.db_path) as db: deleted_paths = [] @@ -156,6 +158,8 @@ async def watch_files(self): for watcher in self.watchers.get(changed_path, []): watcher.notify(change) + self.stopped_watching_files.set() + def watch(self, path: str) -> Watcher: watcher = Watcher(path) self.watchers.setdefault(path, []).append(watcher) diff --git a/plugins/yjs/fps_yjs/main.py b/plugins/yjs/fps_yjs/main.py index 57d567aa..348275d6 100644 --- a/plugins/yjs/fps_yjs/main.py +++ b/plugins/yjs/fps_yjs/main.py @@ -25,4 +25,5 @@ async def start( yield - contents.file_id_manager.watch_files_task.cancel() + contents.file_id_manager.stop_watching_files.set() + await contents.file_id_manager.stopped_watching_files.wait()