Catching control-C

Philip Semanchuk philip at semanchuk.com
Mon Jul 6 18:16:06 EDT 2009


On Jul 6, 2009, at 6:02 PM, Michael Mossey 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?

Hi Mike,
Sorry, I don't know the Python internals well enough to answer your  
question.

Good luck
Philip




More information about the Python-list mailing list