In my previous post on pyinotify I took a short look at the basic use of the pyinotify watcher using mostly default settings. Here I take a brief look at building a custom event processor. The event processor is called to retrieve the event data and, well, process it. The default processor simply prints the output to ‘stdout‘; mine will too. But it is just there to show how simple it is. Note that the processor actually processes the events in line so there’s no need for messy threading.
#!/usr/bin/python
import os
import pyinotify
from datetime import datetime
m = pyinotify.WatchManager()
notifier = pyinotify.Notifier(m, pyinotify.ProcessEvent(), 0, 0, 100)
m.add_watch('/tmp', pyinotify.ALL_EVENTS, rec=True)
while True:
print "START LOOP %s " % datetime.now()
try:
if notifier.check_events():
print "Result of notifier.check_events() = TRUE"
notifier.read_events()
print "Calling notifier.process_events()"
notifier.process_events()
except KeyboardInterrupt:
notifier.stop()
break
print "END LOOP %s " % datetime.now()
This is just the same code as we saw in the earlier post except that I’ve put the ‘notifier.process_events()‘ in its proper place so that it doesn’t get called in every loop execution. It is restricted by the ‘notifier.check_events()‘. To add a little info to the whole process I’ve included some print statements to illustrate the flow of execution. With this we get results that look like:
START LOOP 2009-05-07 23:02:25.025048
Result of notifier.check_events() = TRUE
Calling notifier.process_events()
END LOOP 2009-05-07 23:02:25.104546
Which is what you’d expect. However you will also get results that look like.
START LOOP 2009-05-07 23:02:18.808039
Result of notifier.check_events() = TRUE
Calling notifier.process_events()
END LOOP 2009-05-07 23:02:18.863855
START LOOP 2009-05-07 23:02:18.863912
Result of notifier.check_events() = TRUE
Calling notifier.process_events()
END LOOP 2009-05-07 23:02:18.864656
START LOOP 2009-05-07 23:02:18.864710
Result of notifier.check_events() = TRUE
Calling notifier.process_events()
END LOOP 2009-05-07 23:02:18.865415
So you can see that we get some reasonably unpredictable results.
Now let’s do something a little different and collect the event and print it ourselves a little differently. In this case we’re still collecting all events, we just wanted to mark up the output with something extra.
#!/usr/bin/python
import os
import pyinotify
from datetime import datetime
class e(pyinotify.ProcessEvent):
def process_default(self, event):
print(repr(event))
m = pyinotify.WatchManager()
notifier = pyinotify.Notifier(m, e(), 0, 0, 100)
m.add_watch('/tmp', pyinotify.ALL_EVENTS, rec=True)
while True:
print "START LOOP %s " % datetime.now()
try:
if notifier.check_events():
print "Result of notifier.check_events() = TRUE"
notifier.read_events()
print "Calling notifier.process_events()"
notifier.process_events()
except KeyboardInterrupt:
notifier.stop()
break
print "END LOOP %s " % datetime.now()
Easy huh? But there’s still a little more, there’s a problem! More on that later.