views:

41

answers:

1

The module pyfsevents allows Python programs to utilize the Mac OS X FSEvents framework. One can register a path and a callback function, then call the listen() function, which blocks until a file system event happens in the registered path.

    pyfsevents.registerpath("/example", callback)
    pyfsevents.listen()

I would like to use this module in a multithreaded program, having one thread blocking and waiting for file system events, then notifying another thread to handle the event. Unfortunately, it looks like listen() blocks the entire program, not just the calling thread. Can anyone confirm this? Is there a way to block only the calling thread? If not, are there ways to achieve similar functionality, i.e. letting a thread detect changes in a directory, preferably without polling?

+1  A: 

Yes, as the docs say (under "Limitations"),

Thread-unsafe: CFRunLoop's and Python threads do not quite interact well when put together :)

What I recommend is using multiprocessing to isolate the pyfsevents use in a dedicated process, which can send "heads-up" alerts to the main process in any way you please (e.g., via a multiprocessing.Queue).

If you need to run on Python 2.5, there's a backport of the multiprocessing module -- the latter's in the standard library only since Python 2.6, but the backport runs fine on 2.4 and 2.5.

Alex Martelli