Catching signals in secondary threads

Pier Paolo Glave pierpaolo.glave at ciaolab.com
Wed Aug 28 10:55:32 CEST 2002


Hi everybody,

I'm trying without success to catch a SIGABRT signal in a Python
thread.

I'm running Python 2.2 over Linux (Red Hat 7.2).
In this environment, I see each thread as a separate Linux process,
with its own process id.

I know that the call to signal.signal() should be done only in the
main thread, and this should propagate the signal handling to the
children threads, too.

I tried with the following code:

-------starting code sample------------
import signal
import os
import sys
import time
import threading

def sigAbrtHandler(signo, stackframe):
    """Trapping SIGABRT (possibly coming from Tcl/Tk)
    """
    print 'Trapped SIGABRT!!!'

def testThread():
    print 'Signal handler (secondary) =',
signal.getsignal(signal.SIGABRT)
    while (1):
        time.sleep(5.0)
        print 'Secondary thread is alive', os.getpid()

# Trapping SIGABRT
signal.signal(signal.SIGABRT, sigAbrtHandler)

thr = threading.Thread(target=testThread)
thr.start()

print 'Signal handler (main) =', signal.getsignal(signal.SIGABRT)

while (1):
    time.sleep(5.0)
    print 'Main thread is alive', os.getpid()
-------end of code sample------------
If I kill the main thread with signal 6 (SIGABRT), the message 
'Trapped SIGABRT!!!' is printed out, but not when I send the signal to
the PID of the secondary thread.

Any suggestion for this problem?

Thank you
--
Pier Paolo Glave



More information about the Python-list mailing list