From a8a76bc2278abc14ea7571b4c888a7bb9c9f5ab2 Mon Sep 17 00:00:00 2001 From: Guido Trotter <ultrotter@google.com> Date: Thu, 6 Aug 2009 10:18:41 +0100 Subject: [PATCH] asycnotifier.AsyncNotifier 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 | 1 + lib/asyncnotifier.py | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 lib/asyncnotifier.py diff --git a/Makefile.am b/Makefile.am index b4d3aeaa9..e1dac222c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 000000000..e90fb7cb7 --- /dev/null +++ b/lib/asyncnotifier.py @@ -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() + -- GitLab