From 0bd8d6fa0ccf7faebcde813a1f6d60c7fca482d6 Mon Sep 17 00:00:00 2001 From: LE0N Date: Wed, 3 Oct 2012 22:45:19 +0300 Subject: Add tornado ioloop support --- python2/examples/transient_file_tornado.py | 19 +++++++++++++++++++ python2/pyinotify.py | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 python2/examples/transient_file_tornado.py diff --git a/python2/examples/transient_file_tornado.py b/python2/examples/transient_file_tornado.py new file mode 100644 index 0000000..554f3cd --- /dev/null +++ b/python2/examples/transient_file_tornado.py @@ -0,0 +1,19 @@ +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() \ No newline at end of file diff --git a/python2/pyinotify.py b/python2/pyinotify.py index cd9fc94..6ca990d 100755 --- a/python2/pyinotify.py +++ b/python2/pyinotify.py @@ -1539,6 +1539,25 @@ class AsyncNotifier(asyncore.file_dispatcher, Notifier): self.read_events() 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): + 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): + """ + When asyncore tells us we can read from the fd, we proceed processing + events. This method can be overridden for handling a notification + differently. + + """ + self.read_events() + self.process_events() + class Watch: """ -- cgit v1.2.1