signal module?

Tim Evans t.evans at paradise.net.nz
Thu Mar 27 05:47:44 EST 2003


Daniel Nielsen <djn at daimi.au.dk> writes:

> Hi. 
> 
> I'm currently working on some code that uses the signal module... But
> I doesnt seem to work proberly.
> 
> I'm using threads, so I'd like ctrl-c to actually terminate the
> program. If my memory serves me, ctrl-c is SIGINT, so in my code, in
> the main thread, I write:
> 
> signal.signal(signal.SIGINT, gp.handler);
> 
> where gp is a module and handler looks like:
> 
> def handler(signum, frame):
>     print "Handling signal: " + signum;
>     sys.exit(2);
> 
> But ctrl-c doesnt do anything!
> 
> If I write:
> signal.signal(signal.SIGINT, signal.SIG_DFL);
> instead, ctrl-c actually terminates the program...
> Have I misunderstood something?
> 
> /Daniel

This works fine when for me as long as I press ctrl-c when python is
running and not waiting for input at the '>>>' prompt.  Try typing
this:


>>> while True:
..     pass
..
<press ctrl-c now>
Handling signal: 2


I'm not sure why it works this way, maybe python's use of readline
changes something.

Note that your handler function will not actually work as written.
Replace this line:
    print "Handling signal: " + signum
with this:
    print "Handling signal: ", signum

-- 
Tim Evans




More information about the Python-list mailing list