Interrupting Python

Bengt Richter bokr at oz.net
Sat Sep 7 19:48:11 CEST 2002


On Sat, 7 Sep 2002 06:32:24 -0400, "Bob Easton" <bob at eleaston.com> wrote:

>Yes Peter, it makes sense that PythonWin works that way by design. You are
>also right about running from the command prompt after development/debug is
>done.
>
>For this particular program, using a file semaphore is ideal since I can
>place it at the right point in the program to enable easy restart, something
>not easily done with the random ctrl-break.
You can place a test for Ctrl-C and/or Ctrl-Break at "the right point"(s)
in your program. See below.

>
>Thanks to all who helped clear the fog for me.
>
>--
>Bob Easton
>
>
>"Peter Hansen" <peter at engcorp.com> wrote in message
>news:3d796d22 at news.sentex.net...
>> Bob Easton wrote:
>> > Using catchCtlC.py in a command line environment works as you expect.
>When I
>> > hit Ctrl-Break, Windows displays "^C" and then a command prompt.  If I
>hit
>> > Ctrl-C, the exception handler catches it and displays the results as
>> > specified in catchCtlC/py
>> >
>> > However, my preferred execution environment is PythonWin, not a command
>> > prompt. There, neither Ctrl-break, not Ctrl-C have any effect.  That
>could
>> > easily be a bug with PythonWin.
>>
>> Not a bug, I think.  The whole Ctrl-C/Ctrl-Break concept is only
>> meaningful for command-line ("console") programs, not GUI apps.
>> I don't use PythonWin, but my guess is it does not bother passing
>> ctrl-c on to the application, possibly by design.
>>
>> Can't you just run it under the console?  What advantages do
>> you find by running it under PythonWin other than during
>> development?
>>
>> Also, have you looked at the msvcrt module and kbhit() ?
>>
>> -Peter
>>
>
---< interruptpy.py >------
#!/usr/bin/python
def test():
    import sys, signal
    class SigHandler:
        def __init__(self):
            self.signaled = 0
            self.sn=None
        def __call__(self, sn, sf):
            self.sn = sn 
            self.signaled += 1
    sh = SigHandler()
    old_SIGINT_Handler = signal.signal(signal.SIGINT,sh)
    old_SIGBREAK_Handler = signal.signal(signal.SIGBREAK,sh)
    signames = {
        signal.SIGINT:'SIGINT',
        signal.SIGBREAK:'SIGBREAK'
    }
    print 'Type Ctrl-C or Ctrl-Break to stop'
    while not sh.signaled:   # put this condition in your program loop
        pass                 # (loop body)

    print 'Interrupted loop with %s (not via exception).' % signames[sh.sn]

    signal.signal(signal.SIGINT,old_SIGINT_Handler)
    signal.signal(signal.SIGBREAK,old_SIGBREAK_Handler)

if __name__ == '__main__':
    test()
---------------------------

 >>> from interruptpy import test
 >>> test()
 Type Ctrl-C or Ctrl-Break to stop
 Interrupted loop with SIGINT (not via exception).
 >>> test() # now I'll do ctrl-break
 Type Ctrl-C or Ctrl-Break to stop
 Interrupted loop with SIGBREAK (not via exception).
 >>> ^Z


 [10:55] C:\pywk\pi>interruptpy.py
 Type Ctrl-C or Ctrl-Break to stop
 Interrupted loop with SIGINT (not via exception).

 [10:55] C:\pywk\pi>interruptpy.py
 Type Ctrl-C or Ctrl-Break to stop
 Interrupted loop with SIGBREAK (not via exception).


Obviously you could sprinkle various tests of sh.signaled
into various parts of your code, to check-point variously.
You could resond differently to Ctrl-C vs Ctrl-Break also.

The above is on NT4 Python 2.2 BTW.

BTW2, I suspect I am not the only one who will appreciate it
if you decide to adopt usual ng conventions, and discontinue
top-posting. Consider it a virtual beer for those who paid
attention to your posts anyway ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list