Catching control-C

Simon Forman sajmikins at gmail.com
Tue Jul 7 16:22:48 CEST 2009


On Jul 6, 6:02 pm, Michael Mossey <michaelmos... at gmail.com> wrote:
> On Jul 6, 2:47 pm, Philip Semanchuk <phi... at semanchuk.com> wrote:
>
> > On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote:
>
> > > What is required in a python program to make sure it catches a  
> > > control-
> > > c on the command-line? Do some i/o? The OS here is Linux.
>
> > You can use a try/except to catch a KeyboardInterrupt exception, or  
> > you can trap it using the signal module:http://docs.python.org/library/signal.html
>
> > You want to trap SIGINT.
>
> > HTH
> > Philip
>
> Thanks to both of you. However, my question is also about whether I
> need to be doing i/o or some similar operation for my program to
> notice in any shape or form that Control-C has been pressed. In the
> past, I've written Python programs that go about their business
> ignoring Ctrl-C. Other programs respond to it immediately by exiting.
> I think the difference is that the latter programs are doing i/o. But
> I want to understand better what the "secret" is to responding to a
> ctrl-C in any shape or form.
>
> For example, does trapping SIGINT always work, regardless of what my
> process is doing?
>
> Thanks,
> Mike

Try some experiments. ;]



More information about the Python-list mailing list