Interrupting Python

Bengt Richter bokr at oz.net
Fri Sep 6 19:44:34 EDT 2002


On Fri, 6 Sep 2002 17:38:55 -0400, "Bob Easton" <bob at eleaston.com> wrote:

>Thanks to all.  My system is Win2K. Python simply ignores Ctrl-c and
>Ctrl-break. I think I'll fall back to using file existence as a simple
If this were true as generally as you put it, we should be looking for a bug.

Would you put the following listed catchCtlC.py file in a convenient directory,
and then go there with **a plain "DOS" command window**, and type the command

    catchCtlC.py 1000

It should complete in a short time. Then try it again, hitting Ctrl-C before
it finishes. If you have a fast machine and need more time, add another zero
to the 1000. Let us know if no interrupt is reported for either Ctrl-C or Ctrl-Break.

Then tell us what's different about your program.

---< catchCtlC.py >------------------
i=x=0
def foo(n):
   global i,x
   for i in xrange(n):
       x = i**100

if __name__ == '__main__':
    import sys
    try:
        foo(int(sys.argv[1]))  # call your thing here instead
    except KeyboardInterrupt:
        print 'Interrupted by Ctrl-C'
    else:
        print 'Exited w/o interrupt'
    print 'FHOI, the last i**100 x value: %s**100 = %s' % (i, x)
---< catchCtlC.py >------------------

>semaphore.  That will be good enough and cheap enough for this application.
>

>--
>Bob Easton
>
>
>"David LeBlanc" <whisper at oz.net> wrote in message
>news:mailman.1031345649.9840.python-list at python.org...
>> > I have a script that can run, accessing network resources, for
>> > several days.
>> > Since the script does not normally need keyed input, exception
>processing
>> > does not raise the keyboard exception until after the program
>> > ends normally.
>> > I would like to be able to interrupt if from the keyboard, but have not
>> > learned the trick.  How can I do this?
>> >
>> > --
>> > Bob Easton
>>
>> I have had the experience of Python progs not responding to ctrl-c or
>ctrl-d
>> on Windows, but I found that ctrl-break works every time.
>>
>> Dave LeBlanc
>>
>>
Also, please post responses below what you are responding to ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list