Get rid of `fswatch`
Closed this issue · 4 comments
Instead of using the bash command fswatch
to monitor for changes it would be cleaner and more platform-independend to use https://pypi.org/project/watchfiles/ or a similar python lib.
Tasks:
- Replace fswatch with watchfiles
- Use .syncignore for
awatch()
- specify dependencies "watchfiles" and "pathspec"
- Fix "RuntimeError: Already borrowed" after pressing Ctrl-C
@rodja Since you wrote the setup.py I'd like to assign the two new dependencies for the PyPI package to you.
Maybe you also have an idea about the RuntimeError. I'm out of ideas. But at least it's working regardless of the "graceless" shutdown.
The RuntimeError "Already borrowed" is strange. Watchfiles has a recent issue which is quite similar: samuelcolvin/watchfiles#177. They filed it to pyo3 where it got closed because it just disappears with a new python version: PyO3/pyo3#2525. But in our case the exception also occurs with python 3.10.5.
The documentation of Watchfiles states in awatch:
Unlike watch KeyboardInterrupt cannot be suppressed by awatch so they need to be caught where asyncio.run or equivalent is called.
But if we handle the KeyboardInterrupt around asyncio.run
this internal error is printed:
unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-2' coro=<Folder.watch() done, defined at /Users/rodja/Projects/livesync/livesync/folder.py:58> exception=RuntimeError('Already borrowed')>
Traceback (most recent call last):
File "/Users/rodja/Projects/livesync/livesync/folder.py", line 59, in watch
async for changes in watchfiles.awatch(self.local_dir, stop_event=self._stop_watching,
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/watchfiles/main.py", line 222, in awatch
with RustNotify([str(p) for p in paths], debug, force_polling, poll_delay_ms) as watcher:
RuntimeError: Already borrowed
Unfortunately the RuntimeError occurs before we reach the KeyboardInterrupt. Else we could call stop_watching
on each folder.