Mailing List Archive

signal handler
Hi, I want to use "signal()" in my program.

import signal;
def func(arg1,arg2):
DO_SOMETHING_GOOD;
print "OK!";
signal.signal(SOME_SIGNAL,func);

It seems "func()" is not called
when the program gets signal "SOME_SIGNAL".
Why?
Any help welcome.

(I have installed Python 1.5.2 on Linux 2.0.34 and
running.)

Thanks for reading.
signal handler [ In reply to ]
On Tue, Jun 22, 1999 at 09:55:39PM +0900, Thooney Millennier wrote:
> Newsgroups: comp.lang.python
> Subject: signal handler
> Date: Tue, 22 Jun 1999 21:55:39 +0900
> To: python-list@python.org
>
> import signal;
> def func(arg1,arg2):
> DO_SOMETHING_GOOD;
> print "OK!";
> signal.signal(SOME_SIGNAL,func);
>

Why all those ';'s?

groeten,
Gerrit.

--
The Dutch Linuxgames homepage: http://linuxgames.nl.linux.org
Personal homepage: http://www.nl.linux.org/~gerrit/

Discoverb is a python program in Dutch and English which tests the words you
learned by asking it. Homepage: http://www.nl.linux.org/~gerrit/discoverb/
signal handler [ In reply to ]
Thooney Millennier wrote:
>
> Hi, I want to use "signal()" in my program.
>
> import signal;
> def func(arg1,arg2):
> DO_SOMETHING_GOOD;
> print "OK!";
> signal.signal(SOME_SIGNAL,func);
>
> It seems "func()" is not called
> when the program gets signal "SOME_SIGNAL".
> Why?

It looks like you forgot to send the signal. One way you could implement
this code is:

from signal import signal, SIGINT
from os import getpid, kill

def signalHandler (signum, frame):
print 'signal handler called for signal %d' % (signum,)

signal (SIGINT, signalHandler)
kill (getpid (),

Take a look at the manpage for signal(7) to get the list of signals you
can send to a process.

Hope this helps,
--
Ovidiu Predescu <ovidiu@cup.hp.com>
http://www.geocities.com/SiliconValley/Monitor/7464/
signal handler [ In reply to ]
I am sorry for my late reply,
and thank for your suggestion.
I did't know functions such as
"getpid()","kill()".

Well, I tried and realized...
If you use "kill(getpid(),SIGINT)",
it works. (ie. callback ok!)
Instead,if you send "Control-C" by keyboard input,
it does not. (nothing happens,signal ignored)

It is confusing.

Thooney.

Ovidiu Predescu wrote:
>> Hi, I want to use "signal()" in my program.
>>
>> import signal;
>> def func(arg1,arg2):
>> DO_SOMETHING_GOOD;
>> print "OK!";
>> signal.signal(SOME_SIGNAL,func);
>>
>> It seems "func()" is not called
>> when the program gets signal "SOME_SIGNAL".
>> Why?
>
> It looks like you forgot to send the signal. One way you could
> implement this code is:
>
> from signal import signal, SIGINT
> from os import getpid, kill
>
> def signalHandler (signum, frame):
> print 'signal handler called for signal %d' % (signum,)
>
> signal (SIGINT, signalHandler)
> kill (getpid (),
>
signal handler [ In reply to ]
To catch the Control-C keyboard interrupt, you can just do the following,
depending upon what you are trying to accomplish:

try:
pass # Do Whatever
except KeyboardInterrupt:
print "*** BREAK ***"

It may be the Python interpreter is interfering with your handler,
since it catches the keyboard interrupt and turns it into an exception.

-Jeff Rush


On Mon, 28 Jun 1999 14:30:41, Thooney Millennier <too@pk.highway.ne.jp>
wrote:

> I am sorry for my late reply,
> and thank for your suggestion.
> I did't know functions such as
> "getpid()","kill()".
>
> Well, I tried and realized...
> If you use "kill(getpid(),SIGINT)",
> it works. (ie. callback ok!)
> Instead,if you send "Control-C" by keyboard input,
> it does not. (nothing happens,signal ignored)