Skip to content

Commit c57d32e

Browse files
authored
fix: import asyncinotify sooner to avoid blocking I/O in the event loop (#12)
1 parent f72595e commit c57d32e

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/aiousbwatcher/impl.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
from pathlib import Path
77
from typing import Callable
88

9+
_INOTIFY_EXCEPTION: Exception | None = None
10+
try:
11+
from asyncinotify import Inotify, Mask
12+
except Exception as ex:
13+
_INOTIFY_EXCEPTION = ex
14+
Mask = Inotify = None
15+
16+
917
_PATH = "/dev/bus/usb"
1018

1119
_LOGGER = logging.getLogger(__name__)
@@ -38,12 +46,10 @@ def async_start(self) -> Callable[[], None]:
3846
"""Start the watcher."""
3947
if self._task is not None:
4048
raise RuntimeError("Watcher already started")
41-
try:
42-
from asyncinotify import Inotify # noqa
43-
except Exception as ex:
49+
if _INOTIFY_EXCEPTION is not None:
4450
raise InotifyNotAvailableError(
4551
"Inotify not available on this platform"
46-
) from ex
52+
) from _INOTIFY_EXCEPTION
4753
self._task = self._loop.create_task(self._watcher())
4854
return self._async_stop
4955

@@ -61,8 +67,6 @@ def _async_stop(self) -> None:
6167
self._task = None
6268

6369
async def _watcher(self) -> None:
64-
from asyncinotify import Inotify, Mask
65-
6670
mask = (
6771
Mask.CREATE
6872
| Mask.MOVED_FROM

0 commit comments

Comments
 (0)