Hi all.

There's something a bit tricky about handler_block and subprocess.call I
would like to understand.

I'm writing a little program (a simplistic guitar tuner) that has six keys
(buttons) all connected to a callback that plays sounds through the buzzer
using a call to external function beep. When a key is pressed, beep is
"subprocess.call"ed and plays a sound for 1000 ms.

While the sound is played, further clicks on the key are sort of queued and
played afterwards. If someone gets excited and clicks times and times, then
the program keeps beeping for seconds until it is done. I don't want this.

To avoid this, I figured out I could block the handler while the callback is
calling beep. Therefore, I added calls to handler_block and handler_unblock
at the beginning and end of the callback to disable the call to the callback
itself.

It does not work. It's as if the clicks are dealt with with a delay once the
callback has returned.

Now, as a comparison I tried to add delays inside my callback using the
following code, found on the internet :

        time_start = time.time()
        time_end = (time_start + 1)
        while time_end > time.time():
                while gtk.events_pending():
                        gtk.main_iteration()

If I replace the call to beep by this delay, I get the expected result : the
clicks are ignored when the handlers are blocked.

-> There is something here that is specific to subprocess.call().

Now, if I have both the delay and the call to beep, it goes like :

def callback(...):

    disconnect_handlers
    print "1"
    delay 1000 ms
    print "2"
    call to beep 1000 ms
    print "3"
    reconnect_handlers

If I click several times on a key while between 1 and 2, nothing happens. If
I click several times on a key while between 2 and 3, I get a second beep
after the current, but only one (used to be several).

Then, I add a second delay, this time after the call to beep :

def callback(...):

    disconnect_handlers
    print "1"
    delay 1000 ms
    print "2"
    call to beep 1000 ms
    print "3"
    delay 1000 ms
    reconnect_handlers

Now, I get what I expected : the clicks on the keys between 2 and 3 are
ignored (or so they seem).

I can even shorten the delay to 1 ms and it still works as I want it. I guess
we could call that a workaround. Yet, I'm not satisfied. I doesn't seem very
clean. And anyway, I don't really care about the program itself, I do it as
an exercise and I would like to understand.

Could anybody point me to a ressource that would explain that to me ? I went
through pyGTK and python tutorials but I didn't find any answer and it seems a
bit tricky. Still, I believe there should be an explanation for all this and a
cleaner way to proceed.

Thanks !

-- 
Jérôme
_______________________________________________
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to