Interrupting Python
Bob Easton
bob at eleaston.com
Fri Sep 6 20:50:45 EDT 2002
OS = Win2000
Python = 2.2
Python execution environment = PythonWin
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.
--
Bob Easton
"Bengt Richter" <bokr at oz.net> wrote in message
news:albel2$7te$0 at 216.39.172.122...
> 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