summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xpython2/pyinotify.py37
-rw-r--r--python3/examples/asyncio_notifier.py20
-rwxr-xr-xpython3/pyinotify.py33
3 files changed, 88 insertions, 2 deletions
diff --git a/python2/pyinotify.py b/python2/pyinotify.py
index 707c8e3..13af04e 100755
--- a/python2/pyinotify.py
+++ b/python2/pyinotify.py
@@ -1162,7 +1162,7 @@ class Notifier:
At least with read_freq set you might sleep.
@type threshold: int
@param timeout:
- http://docs.python.org/lib/poll-objects.html#poll-objects
+ https://docs.python.org/3/library/select.html#polling-objects
@type timeout: int
"""
# Watch Manager instance
@@ -1482,7 +1482,7 @@ class ThreadedNotifier(threading.Thread, Notifier):
least with read_freq you might sleep.
@type threshold: int
@param timeout:
- see http://docs.python.org/lib/poll-objects.html#poll-objects
+ https://docs.python.org/3/library/select.html#polling-objects
@type timeout: int
"""
# Init threading base class
@@ -1609,6 +1609,39 @@ class TornadoAsyncNotifier(Notifier):
self.handle_read_callback(self)
+class AsyncioNotifier(Notifier):
+ """
+
+ asyncio/trollius event loop adapter.
+
+ """
+ def __init__(self, watch_manager, loop, callback=None,
+ default_proc_fun=None, read_freq=0, threshold=0, timeout=None):
+ """
+
+ See examples/asyncio_notifier.py for an example usage.
+
+ @param loop: asyncio or trollius event loop instance.
+ @type loop: asyncio.BaseEventLoop or trollius.BaseEventLoop instance.
+ @param callback: Functor called at the end of each call to handle_read.
+ Expects to receive the notifier object (self) as
+ single parameter.
+ @type callback: callable object or function
+
+ """
+ self.loop = loop
+ self.handle_read_callback = callback
+ Notifier.__init__(self, watch_manager, default_proc_fun, read_freq,
+ threshold, timeout)
+ loop.add_reader(self._fd, self.handle_read)
+
+ def handle_read(self, *args, **kwargs):
+ self.read_events()
+ self.process_events()
+ if self.handle_read_callback is not None:
+ self.handle_read_callback(self)
+
+
class Watch:
"""
Represent a watch, i.e. a file or directory being watched.
diff --git a/python3/examples/asyncio_notifier.py b/python3/examples/asyncio_notifier.py
new file mode 100644
index 0000000..16be533
--- /dev/null
+++ b/python3/examples/asyncio_notifier.py
@@ -0,0 +1,20 @@
+import pyinotify
+import asyncio
+
+
+def handle_read_callback(notifier):
+ """
+ Just stop receiving IO read events after the first
+ iteration (unrealistic example).
+ """
+ print('handle_read callback')
+ notifier.loop.stop()
+
+
+wm = pyinotify.WatchManager()
+loop = asyncio.get_event_loop()
+notifier = pyinotify.AsyncioNotifier(wm, loop,
+ callback=handle_read_callback)
+wm.add_watch('/tmp', pyinotify.ALL_EVENTS)
+loop.run_forever()
+notifier.stop()
diff --git a/python3/pyinotify.py b/python3/pyinotify.py
index f604385..4f039f5 100755
--- a/python3/pyinotify.py
+++ b/python3/pyinotify.py
@@ -1599,6 +1599,39 @@ class TornadoAsyncNotifier(Notifier):
self.handle_read_callback(self)
+class AsyncioNotifier(Notifier):
+ """
+
+ asyncio/trollius event loop adapter.
+
+ """
+ def __init__(self, watch_manager, loop, callback=None,
+ default_proc_fun=None, read_freq=0, threshold=0, timeout=None):
+ """
+
+ See examples/asyncio_notifier.py for an example usage.
+
+ @param loop: asyncio or trollius event loop instance.
+ @type loop: asyncio.BaseEventLoop or trollius.BaseEventLoop instance.
+ @param callback: Functor called at the end of each call to handle_read.
+ Expects to receive the notifier object (self) as
+ single parameter.
+ @type callback: callable object or function
+
+ """
+ self.loop = loop
+ self.handle_read_callback = callback
+ Notifier.__init__(self, watch_manager, default_proc_fun, read_freq,
+ threshold, timeout)
+ loop.add_reader(self._fd, self.handle_read)
+
+ def handle_read(self, *args, **kwargs):
+ self.read_events()
+ self.process_events()
+ if self.handle_read_callback is not None:
+ self.handle_read_callback(self)
+
+
class Watch:
"""
Represent a watch, i.e. a file or directory being watched.