[Idle-dev] Re: [Tutor] How to interrupt in IDLE (fwd)

Mike Clarkson michael@internetdiscovery.com
Mon, 15 Apr 2002 00:18:48 -0400


At 10:13 PM 4/9/02 -0400, Guido van Rossum wrote:
>> Hi everyone,
>> 
>> Roman Suzuki from Python-tutor recently discovered that Ctrl-C
>> doesn't work in IDLE if the system is busy-waiting.  I'm forwarding
>> his post here.
>
>Thanks.  Unfortunately, there's no real easy way out of this.
>The ^C can only be detected by Tkinter's event loop, and that isn't
>active when the Python VM is executing code, only when it's asking for
>input or printing output.

I think that there is one easy way out of this, but really the Tkinter
mainloop C code should be fixed to get around the 30 msec. sleep in the
Tkinter mainloop. The sleep in the loop creates dead time, causes CPU burn
because it isn't using some kind of select call, and won't process Python
signals if it's vwait waiting in Tk. I have advocated scheduling something
regularly on the Tcl event queue to check for Python signals, using a
function already in _tkinter.c - Tkapp_CreateTimerHandler.

Although I think this is technically the correct way to do it, there is an
easy route that may solve your problem: do the mainloop in Python. 

        self.exit = -1
        while self.exit < 0:
            self.tk.dooneevent(0)

where self.tk.quit() is changed to set self.exit to 0. This has no sleep,
no dead time, reacts to Keyboard Interupt after any Tcl event (e.g. mouse
move), and has the added advantage of being able to easily add hooks into
the main loop to poll for other things:

        self.exit = -1
        while self.exit < 0:
           self.tk.dooneevent(0)
           # plug in any other polling checks here: asyncore ...
           if self.hooks is not None: map (eval, self.hooks)

This seems to work well, though I haven't tested it using threads. 
To see examples of this approach, I think the CVS version of the Tix demos 
have been switched over to use this:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python/
python/dist/src/Demo/tix/samples/SHList2.py?
rev=1.3&content-type=text/vnd.viewcvs-markup

Mike.