Skip to content

Commit

Permalink
Add tornado ioloop support
Browse files Browse the repository at this point in the history
  • Loading branch information
Flyguy committed Oct 3, 2012
1 parent 66c3954 commit 0bd8d6f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions python2/examples/transient_file_tornado.py
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 19 additions & 0 deletions python2/pyinotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,25 @@ def handle_read(self):
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:
"""
Expand Down

0 comments on commit 0bd8d6f

Please sign in to comment.