From aea39987fa5f3efaad15c2fa4fe2597996393327 Mon Sep 17 00:00:00 2001 From: Sebastien Martini Date: Thu, 4 Oct 2012 12:18:04 +0200 Subject: Improved Tornado's notifier. --- python2/examples/tornado_notifier.py | 21 +++++++++++++++++++++ python2/examples/transient_file_tornado.py | 20 -------------------- python2/pyinotify.py | 23 +++++++++++++++++++---- python3/pyinotify.py | 22 +++++++++++++++++----- 4 files changed, 57 insertions(+), 29 deletions(-) create mode 100644 python2/examples/tornado_notifier.py delete mode 100644 python2/examples/transient_file_tornado.py diff --git a/python2/examples/tornado_notifier.py b/python2/examples/tornado_notifier.py new file mode 100644 index 0000000..9390adc --- /dev/null +++ b/python2/examples/tornado_notifier.py @@ -0,0 +1,21 @@ +import pyinotify +from tornado.ioloop import IOLoop + + +def handle_read_callback(notifier): + """ + Just stop receiving IO read events after the first + iteration (unrealistic example). + """ + print('handle_read callback') + notifier.io_loop.stop() + + +wm = pyinotify.WatchManager() +ioloop = IOLoop.instance() +notifier = pyinotify.TornadoAsyncNotifier(wm, ioloop, + callback=handle_read_callback) +wm.add_watch('/tmp', pyinotify.ALL_EVENTS) +ioloop.start() +ioloop.close() +notifier.stop() diff --git a/python2/examples/transient_file_tornado.py b/python2/examples/transient_file_tornado.py deleted file mode 100644 index a3347d3..0000000 --- a/python2/examples/transient_file_tornado.py +++ /dev/null @@ -1,20 +0,0 @@ -import pyinotify -from tornado.ioloop import IOLoop - -wm = pyinotify.WatchManager() # Watch Manager -mask = pyinotify.IN_MODIFY - -class EventHandler(pyinotify.ProcessEvent): - def process_IN_MODIFY(self, event): - # We have explicitely registered for this kind of event. - print '\t', event.pathname, ' -> written' - def process_default(self, event): - print event - -ioloop = IOLoop.instance() -notifier = pyinotify.TornadoAsyncNotifier(wm, ioloop) -#daemon.pids['nginx'] -wdd = wm.watch_transient_file('/tmp/test_file', pyinotify.IN_MODIFY, - EventHandler) - -ioloop.start() diff --git a/python2/pyinotify.py b/python2/pyinotify.py index 82592a8..f03de1b 100755 --- a/python2/pyinotify.py +++ b/python2/pyinotify.py @@ -1543,18 +1543,30 @@ class AsyncNotifier(asyncore.file_dispatcher, Notifier): class TornadoAsyncNotifier(Notifier): """ Tornado ioloop adapter. + """ - def __init__(self, watch_manager, ioloop, default_proc_fun=None, - read_freq=0, threshold=0, timeout=None, channel_map=None): + def __init__(self, watch_manager, ioloop, callback=None, + default_proc_fun=None, read_freq=0, threshold=0, timeout=None, + channel_map=None): """ - See example transient_file_tornado.py + Note that if later you must call ioloop.close() be sure to let the + default parameter to all_fds=False. + + See example transient_file_tornado.py for an example using this + notifier. @param ioloop: Tornado's IO loop. @type ioloop: tornado.ioloop.IOLoop instance. + @param callback: Functor called at the end of each call to handle_read + (IOLoop's read handler). Expects to receive the + notifier object (self) as single parameter. + @type callback: callable object or function """ + self.io_loop = ioloop + self.handle_read_callback = callback Notifier.__init__(self, watch_manager, default_proc_fun, read_freq, threshold, timeout) - ioloop.add_handler(os.dup(self._fd), self.handle_read, ioloop.READ) + ioloop.add_handler(self._fd, self.handle_read, ioloop.READ) def handle_read(self, *args, **kwargs): """ @@ -1563,6 +1575,8 @@ class TornadoAsyncNotifier(Notifier): """ self.read_events() self.process_events() + if self.handle_read_callback is not None: + self.handle_read_callback(self) class Watch: @@ -1621,6 +1635,7 @@ class Watch: class ExcludeFilter: """ ExcludeFilter is an exclusion filter. + """ def __init__(self, arg_lst): """ diff --git a/python3/pyinotify.py b/python3/pyinotify.py index 6c52708..76ef043 100755 --- a/python3/pyinotify.py +++ b/python3/pyinotify.py @@ -1540,18 +1540,28 @@ class TornadoAsyncNotifier(Notifier): Tornado ioloop adapter. """ - def __init__(self, watch_manager, ioloop, default_proc_fun=None, - read_freq=0, threshold=0, timeout=None, channel_map=None): + def __init__(self, watch_manager, ioloop, callback=None, + default_proc_fun=None, read_freq=0, threshold=0, timeout=None, + channel_map=None): """ - See example transient_file_tornado.py + Note that if later you must call ioloop.close() be sure to let the + default parameter to all_fds=False. + + See example transient_file_tornado.py for an example using this + notifier. @param ioloop: Tornado's IO loop. @type ioloop: tornado.ioloop.IOLoop instance. - + @param callback: Functor called at the end of each call to handle_read + (IOLoop's read handler). Expects to receive the + notifier object (self) as single parameter. + @type callback: callable object or function """ + self.io_loop = ioloop + self.handle_read_callback = callback Notifier.__init__(self, watch_manager, default_proc_fun, read_freq, threshold, timeout) - ioloop.add_handler(os.dup(self._fd), self.handle_read, ioloop.READ) + ioloop.add_handler(self._fd, self.handle_read, ioloop.READ) def handle_read(self, *args, **kwargs): """ @@ -1560,6 +1570,8 @@ class TornadoAsyncNotifier(Notifier): """ self.read_events() self.process_events() + if self.handle_read_callback is not None: + self.handle_read_callback(self) class Watch: -- cgit v1.2.1