From 75f63f3f63ed1105b5cad98e1121dc55e5043cba Mon Sep 17 00:00:00 2001 From: Sebastien Martini Date: Thu, 4 Oct 2012 00:45:11 +0200 Subject: Ported TornadoAsyncNotifier to Python3. Assuming Tornado works the same way under Python3... I must say I don't use Tornado myself I didn't test it. --- python2/examples/transient_file_tornado.py | 5 +++-- python2/pyinotify.py | 20 +++++++++++++------- python3/pyinotify.py | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/python2/examples/transient_file_tornado.py b/python2/examples/transient_file_tornado.py index 554f3cd..a3347d3 100644 --- a/python2/examples/transient_file_tornado.py +++ b/python2/examples/transient_file_tornado.py @@ -14,6 +14,7 @@ class EventHandler(pyinotify.ProcessEvent): ioloop = IOLoop.instance() notifier = pyinotify.TornadoAsyncNotifier(wm, ioloop) #daemon.pids['nginx'] -wdd = wm.watch_transient_file('/tmp/test_file', pyinotify.IN_MODIFY, EventHandler) +wdd = wm.watch_transient_file('/tmp/test_file', pyinotify.IN_MODIFY, + EventHandler) -ioloop.start() \ No newline at end of file +ioloop.start() diff --git a/python2/pyinotify.py b/python2/pyinotify.py index 6ca990d..82592a8 100755 --- a/python2/pyinotify.py +++ b/python2/pyinotify.py @@ -1539,20 +1539,26 @@ class AsyncNotifier(asyncore.file_dispatcher, Notifier): self.read_events() self.process_events() + class TornadoAsyncNotifier(Notifier): """ - Tornado ioloop adapter + 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, default_proc_fun=None, + read_freq=0, threshold=0, timeout=None, channel_map=None): + """ + See example transient_file_tornado.py + + @param ioloop: Tornado's IO loop. + @type ioloop: tornado.ioloop.IOLoop instance. + """ Notifier.__init__(self, watch_manager, default_proc_fun, read_freq, threshold, timeout) ioloop.add_handler(os.dup(self._fd), self.handle_read, ioloop.READ) - def handle_read(self,*args,**kwargs): + + def handle_read(self, *args, **kwargs): """ - When asyncore tells us we can read from the fd, we proceed processing - events. This method can be overridden for handling a notification - differently. + See comment in AsyncNotifier. """ self.read_events() diff --git a/python3/pyinotify.py b/python3/pyinotify.py index a826efb..6c52708 100755 --- a/python3/pyinotify.py +++ b/python3/pyinotify.py @@ -1535,6 +1535,33 @@ class AsyncNotifier(asyncore.file_dispatcher, Notifier): self.process_events() +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): + """ + See example transient_file_tornado.py + + @param ioloop: Tornado's IO loop. + @type ioloop: tornado.ioloop.IOLoop instance. + + """ + Notifier.__init__(self, watch_manager, default_proc_fun, read_freq, + threshold, timeout) + ioloop.add_handler(os.dup(self._fd), self.handle_read, ioloop.READ) + + def handle_read(self, *args, **kwargs): + """ + See comment in AsyncNotifier. + + """ + self.read_events() + self.process_events() + + class Watch: """ Represent a watch, i.e. a file or directory being watched. -- cgit v1.2.1