Reputation: 40962
I have some basic questions about pyinotify that I can't seem to find the answers to elsewhere.
1) For a continuous directory monitor (and event processor) is it necessary to have a while( True ) loop or is the continuous event 'loop' handled by the notify watch and ended when I remove the watch?
2) What happens if files are pre-existing when the inotify instance is 'turned-on'? Initially I just want to monitor for IN_CREATE but this won't handle pre-existing files.
3) Similar to #2, what happens if a file gets created while I'm in my event processor function? Will pyinotify cache it in its queue and process it when the 'loop' starts again, or will I lose this event?
Upvotes: 1
Views: 2262
Reputation: 880787
You'll need a while-loop
, but it can be set up implicitly by calling the notifier.loop
method:
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CREATE
notifier = pyinotify.Notifier(wm, MyProcessEvent(path))
wdd = wm.add_watch(path, mask, rec=True, auto_add=True)
notifier.loop()
If you wish to set up the while-loop
yourself, you might start with this source code from notifier.loop
:
while 1:
try:
notifier.process_events()
# check_events is blocking
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
# Stop monitoring if sigint is caught (Control-C).
break
notifier.stop()
To remove a watch of a particular file or directory, call wm.rm_watch
.
What happens if files are pre-existing when the inotify instance is 'turned-on'?
No event is generated before wm.add_watch
is called.
what happens if a file gets created while I'm in my event processor function?
The events are queued in a buffer of size
/proc/sys/fs/inotify/max_queued_events
. For example, on my system
that number is
% cat /proc/sys/fs/inotify/max_queued_events
16384
If the filesystem generates enough events to fill up the buffer while
you are processing a prior event, then you get a IN_Q_OVERFLOW
event.
See the comment in blucz's answer.
Upvotes: 2