Skip to content

Commit

Permalink
Merge pull request seb-m#85 from dano/master
Browse files Browse the repository at this point in the history
Add asyncio-compatible Notifier sub-class.
  • Loading branch information
seb-m committed Dec 22, 2014
2 parents 73a7346 + 6e77a4c commit af50b62
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
33 changes: 33 additions & 0 deletions python2/pyinotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,39 @@ def handle_read(self, *args, **kwargs):
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.
Expand Down
20 changes: 20 additions & 0 deletions python3/examples/asyncio_notifier.py
Original file line number Diff line number Diff line change
@@ -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()
33 changes: 33 additions & 0 deletions python3/pyinotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,39 @@ def handle_read(self, *args, **kwargs):
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.
Expand Down

0 comments on commit af50b62

Please sign in to comment.