asycnotifier.AsyncNotifier
authorGuido Trotter <ultrotter@google.com>
Thu, 6 Aug 2009 09:18:41 +0000 (10:18 +0100)
committerGuido Trotter <ultrotter@google.com>
Fri, 7 Aug 2009 09:08:27 +0000 (10:08 +0100)
AsyncNotifier is a special asyncore class that delivers inotify events
asynchronously.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

Makefile.am
lib/asyncnotifier.py [new file with mode: 0644]

index b4d3aea..e1dac22 100644 (file)
@@ -65,6 +65,7 @@ nodist_pkgpython_PYTHON = \
 
 pkgpython_PYTHON = \
        lib/__init__.py \
+       lib/asyncnotifier.py \
        lib/backend.py \
        lib/bdev.py \
        lib/bootstrap.py \
diff --git a/lib/asyncnotifier.py b/lib/asyncnotifier.py
new file mode 100644 (file)
index 0000000..e90fb7c
--- /dev/null
@@ -0,0 +1,55 @@
+#
+#
+
+# Copyright (C) 2009 Google Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+
+"""Asynchronous pyinotify implementation"""
+
+
+import pyinotify
+import asyncore
+
+
+class AsyncNotifier(asyncore.file_dispatcher):
+  """An asyncore dispatcher for inotify events.
+
+  """
+
+  def __init__(self, watch_manager,
+               default_proc_fun=None,
+               map=None):
+    """
+    Constructor for AsyncNotifier, a special asyncore file_dispatcher that
+    actually wraps a pyinotify Notifier, making it asyncronous.
+
+    """
+    if default_proc_fun is None:
+      default_proc_fun=pyinotify.ProcessEvent()
+    self.notifier = pyinotify.Notifier(watch_manager, default_proc_fun)
+    # here we need to steal the file descriptor from the notifier, so we can
+    # use it in the global asyncore select, and avoid calling the
+    # check_events() function of the notifier (which doesn't allow us to select
+    # together with other file descriptors)
+    self.fd = self.notifier._fd
+    asyncore.file_dispatcher.__init__(self, self.fd, map)
+
+  def handle_read(self):
+    self.notifier.read_events()
+    self.notifier.process_events()
+