From b1da9d65979ed50eca42d68814fd99badece1de3 Mon Sep 17 00:00:00 2001 From: dano Date: Thu, 16 Oct 2014 19:40:49 -0400 Subject: Add asyncio Notifier adapter --- python2/pyinotify.py | 31 +++++++++++++++++++++++++++++++ python3/pyinotify.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/python2/pyinotify.py b/python2/pyinotify.py index 7733337..a12ce3f 100755 --- a/python2/pyinotify.py +++ b/python2/pyinotify.py @@ -1604,6 +1604,37 @@ 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): + """ + + @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/pyinotify.py b/python3/pyinotify.py index 57c3549..407b74a 100755 --- a/python3/pyinotify.py +++ b/python3/pyinotify.py @@ -1594,6 +1594,37 @@ 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): + """ + + @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. -- cgit v1.2.1 From c297e50a2ae0c1cdd2380a395a3ed37199580506 Mon Sep 17 00:00:00 2001 From: dano Date: Thu, 16 Oct 2014 19:43:52 -0400 Subject: Add asyncio example --- python3/examples/asyncio_notifier.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 python3/examples/asyncio_notifier.py 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() -- cgit v1.2.1 From 6e77a4c8d001971cec6c98c6af8e553872a121d4 Mon Sep 17 00:00:00 2001 From: dano Date: Thu, 16 Oct 2014 19:44:55 -0400 Subject: doc update --- python2/pyinotify.py | 2 ++ python3/pyinotify.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/python2/pyinotify.py b/python2/pyinotify.py index a12ce3f..089b636 100755 --- a/python2/pyinotify.py +++ b/python2/pyinotify.py @@ -1614,6 +1614,8 @@ class AsyncioNotifier(Notifier): 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. diff --git a/python3/pyinotify.py b/python3/pyinotify.py index 407b74a..a6601c8 100755 --- a/python3/pyinotify.py +++ b/python3/pyinotify.py @@ -1604,6 +1604,8 @@ class AsyncioNotifier(Notifier): 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. -- cgit v1.2.1