Skip to content

Commit

Permalink
Use watchfiles stop_event
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Mar 21, 2023
1 parent b70151d commit 783aa26
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 10 additions & 6 deletions plugins/contents/fps_contents/fileid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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:
Expand All @@ -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 = []
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion plugins/yjs/fps_yjs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 783aa26

Please sign in to comment.