Blocking code

E

eric.frederich

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.
Pressing Ctrl-C does nothing here.
This seems to be the behavior of FAM so it was re-implemented in
Gamin.
I looked at another set of bindings for FAM in Java.
It came with an example that watched a directory for changes. They
basically put the code that blocks in another thread and accepted
input on a second thread waiting for the user to press q and it would
kill the blocking thread.

I don't know how to do something like this in Python, I have never
used threads and I'm not sure if thats the way to go.

Someone else that complained about the blocking behavior of those
calls said that he found a solution since he was using OCaml.
Apparently OCaml has a way to say that you're entering a block of code
that blocks and you can still exit using Ctrl-C.
I don't think anything like this exists in Python does it?

So, my question here is.... How from Python can I call code that
blocks without losing the ability to use Ctrl-C?

Here is the code that is impenetrable to Ctrl-C. You need to kill it
with another terminal....

#!/usr/bin/env python

import gamin
import sys

directory = sys.argv[1]

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

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

while True:
mon.handle_one_event()
 
A

Adam Tauno Williams

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.
Pressing Ctrl-C does nothing here.
This seems to be the behavior of FAM so it was re-implemented in
Gamin.


Hmmm, I don't know if I believe that.
I looked at another set of bindings for FAM in Java.
It came with an example that watched a directory for changes. They
basically put the code that blocks in another thread and accepted
input on a second thread waiting for the user to press q and it would
kill the blocking thread.

You call is probably doing a select(...) on the file handle that FAM (or
whatever) is going to call back on. You could probably override that
method and add a timeout. Then do-whatever... wait-and-listen...
do-whatever. This works very well in many cases, you won't loose
events, they will still be there when you go back to check.
I don't know how to do something like this in Python, I have never
used threads and I'm not sure if thats the way to go.

In Python I'd use multiprocessing rather than threads.
<http://docs.python.org/library/multiprocessing.html>
 
P

Peter Otten

eric.frederich said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top