Blocking code

Peter Otten __peter__ at web.de
Fri Jan 15 14:16:55 EST 2010


eric.frederich wrote:

> I am trying to write something that will watch directories without
> poling them.
> This is what FAM is fore.  Gamin is a re-implementation of FAM and it
> has python bindings.
> 
> The problem is that when I call handle_one_event() it blocks until
> there is an event to handle.

What's the purpose of mon.event_pending()? Can you use it to check whether 
an event is due? Example:

import gamin
import sys
import time

directory = sys.argv[1]

def callback(path, event):
    print "Got callback: %s, %s" % (path, event)

mon = gamin.WatchMonitor()
mon.watch_directory(directory, callback)

while True:
    if mon.event_pending():
        mon.handle_one_event()
    else:
        time.sleep(.1)

I didn't find anything that deserves to be called documentation, but a quick 
test suggests that this at least responds to Ctrl-C:

$ mkdir alpha
$ python gamin_test.py alpha/ &
[1] 9932
$ Got callback: /home/petto/srcx/clpy/alpha, 8
Got callback: /home/petto/srcx/clpy/alpha, 9
touch alpha/beta
$ Got callback: beta, 5
Got callback: beta, 1
mkdir alpha/gamma
$ Got callback: gamma, 5
fg
python gamin_test.py alpha/
^CTraceback (most recent call last):
  File "gamin_test.py", line 19, in <module>
    time.sleep(.1)
KeyboardInterrupt

Peter




More information about the Python-list mailing list