Inotify for Python

Submitted by Hannes Schmidt on Thu, 01/24/2013 - 13:27.

The other day, I was trying to use Linux' inotify(7) functionality in a Python script. The obvious contender seems to be pynotify, being the top Google result, as well as being mentioned in the O'Reilly title Python for Unix and Linux System Administration. Unfortunately, I found myself dumbfounded by its API. The native inotify(7) functionality is simple and elegant, what does a scripting wrapper have to add on top of it? If it were an abstraction that unifies the various platform-specific filesystem notification approaches, I would be able to digest a thick, opinionated API, but a mere wrapper for one particular platform should be just that: a wrapper, as thin as possible.

So I kept looking, and I won't bore you with the details, but ultimately I chose Bryan O'Sullivan's python-inotify. Its meat is efficiently implemented in C. Other inotify wrappers for Python use ctypes which probably (guilty as charged, I did not measure) adds some overhead. I also came across a polling implementation which used a hard-coded sleep time of one second, which is hilarious. Bryan's wrapper is blocking, not polling, and it fulfills my requirement for being thin. Unfortunately, it hasn't been maintained in a while, which led to another fork by JanKanis with useful improvements and fixes. I looked at a bug in the original and decided to fix it, which I did in my own fork. The corresponding pull request is on JanKanis' fork.