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 20, 2023
1 parent b70151d commit a69d111
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
6 changes: 5 additions & 1 deletion 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 @@ -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()
4 changes: 2 additions & 2 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async def test_rest_api(start_jupyverse):
ydoc = Y.YDoc()
WebsocketProvider(ydoc, websocket)
# wait for file to be loaded and Y model to be created in server and client
await asyncio.sleep(0.5)
await asyncio.sleep(1)
# execute notebook
for cell_idx in range(3):
response = requests.post(
Expand All @@ -98,7 +98,7 @@ async def test_rest_api(start_jupyverse):
)
print(f"{url}/api/kernels/{kernel_id}/execute", response.json())
# wait for Y model to be updated
await asyncio.sleep(0.5)
await asyncio.sleep(1)
# retrieve cells
cells = json.loads(ydoc.get_array("cells").to_json())
assert cells[0]["outputs"] == [
Expand Down

0 comments on commit a69d111

Please sign in to comment.