summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastien Martini <seb@dbzteam.org>2009-06-16 23:43:46 +0200
committerSebastien Martini <seb@dbzteam.org>2009-06-16 23:43:46 +0200
commit17a351fb6e7b1d5a9455d52ab82e02aba2b95091 (patch)
treeacd31891349bb489f44d062f76204253921a1308
parent6c5f348a4bd4c452a131da77f898253da9fc8b83 (diff)
downloadpyinotify-17a351fb6e7b1d5a9455d52ab82e02aba2b95091.tar.gz
Updated example stats_threaded.py and updated various comments into
pyinotify.py
-rw-r--r--examples/stats_threaded.py30
-rwxr-xr-xpyinotify.py23
2 files changed, 36 insertions, 17 deletions
diff --git a/examples/stats_threaded.py b/examples/stats_threaded.py
index 7ead427..8d3ec85 100644
--- a/examples/stats_threaded.py
+++ b/examples/stats_threaded.py
@@ -10,28 +10,38 @@ from pyinotify import *
class Identity(ProcessEvent):
-
def process_default(self, event):
# Does nothing, just to demonstrate how stuffs could be done
# after having processed statistics.
pass
+# Thread #1
+wm1 = WatchManager()
+s1 = Stats() # Stats is a subclass of ProcessEvent
+notifier1 = ThreadedNotifier(wm1, default_proc_fun=Identity(s1))
+notifier1.start()
+wm1.add_watch('/tmp/', ALL_EVENTS, rec=True, auto_add=True)
-wm = WatchManager()
-s = Stats() # Stats is a subclass of ProcessEvent
-notifier = ThreadedNotifier(wm, default_proc_fun=Identity(s))
-notifier.start()
-wm.add_watch('/tmp/', ALL_EVENTS, rec=True, auto_add=True)
+# Thread #2
+wm2 = WatchManager()
+s2 = Stats() # Stats is a subclass of ProcessEvent
+notifier2 = ThreadedNotifier(wm2, default_proc_fun=Identity(s2))
+notifier2.start()
+wm2.add_watch('/var/log/', ALL_EVENTS, rec=False, auto_add=False)
while True:
try:
- print repr(s)
- print s
+ print "Thread 1", repr(s1)
+ print s1
+ print "Thread 2", repr(s2)
+ print s2
print
time.sleep(5)
except KeyboardInterrupt:
- notifier.stop()
+ notifier1.stop()
+ notifier2.stop()
break
except:
- notifier.stop()
+ notifier1.stop()
+ notifier2.stop()
raise
diff --git a/pyinotify.py b/pyinotify.py
index b4f0539..f41360a 100755
--- a/pyinotify.py
+++ b/pyinotify.py
@@ -1384,7 +1384,10 @@ class WatchManagerError(Exception):
class WatchManager:
"""
Provide operations for watching files and directories. Integrated
- dictionary is used to reference watched items.
+ dictionary is used to reference watched items. When used inside
+ threaded code, instanciates a new WatchManager instance for each
+ ThreadedNotifier.
+
"""
def __init__(self, exclude_filter=lambda path: False):
"""
@@ -1408,7 +1411,7 @@ class WatchManager:
Add a watch on path, build a Watch object and insert it in the
watch manager dictionary. Return the wd value.
"""
- # Unicode strings are converted to bytes strings, it seems to be
+ # Unicode strings are converted to byte strings, it seems to be
# required because LIBC.inotify_add_watch does not work well when
# it receives an ctypes.create_unicode_buffer instance as argument.
# Therefore even wd are indexed with bytes string and not with
@@ -1439,12 +1442,17 @@ class WatchManager:
auto_add=False, do_glob=False, quiet=True,
exclude_filter=None):
"""
- Add watch(s) on given path(s) with the specified mask and
- optionnally with a processing function and recursive flag.
+ Add watch(s) on given |path|(s) with the specified |mask| and
+ optionnally with a processing |proc_fun| function and a recursive
+ flag |rec|.
+ Ideally |path| components should not be unicode objects. Note
+ that unicode paths are accepted but are converted to byte strings
+ before a watch is put on the path. The encoding used for converting
+ the unicode object is given by sys.getfilesystemencoding().
@param path: Path to watch, the path can either be a file or a
directory. Also accepts a sequence (list) of paths.
- @type path: string or list of string
+ @type path: string or list of strings
@param mask: Bitmask of events.
@type mask: int
@param proc_fun: Processing object.
@@ -1539,8 +1547,9 @@ class WatchManager:
def update_watch(self, wd, mask=None, proc_fun=None, rec=False,
auto_add=False, quiet=True):
"""
- Update existing watch(s). Both the mask and the processing
- object can be modified.
+ Update existing watch(s). The |mask|, the processing object
+ |proc_fun|, the recursive param |rec| and the |auto_add| and
+ |quiet| flags can be updated.
@param wd: Watch Descriptor to update. Also accepts a list of
watch descriptors.