signal module?
Daniel Nielsen
djn at daimi.au.dk
Thu Mar 27 06:28:58 EST 2003
On 27/03-03 10.47, Tim Evans wrote:
> 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
>
I'm not using the python cmd line, instead i run it from a script.
I tried NOT starting seperate threads, and then it works as
expected. No problems.
But as soon as I reenable the threads, the handler doesn't get the
signal... I cannot understand why! Anyone care to elaborate?
/Daniel
--
There are no great men, only great challenges that ordinary men are forced
by circumstances to meet.
-- Admiral William Halsey
More information about the Python-list
mailing list