Problem with threads and signals

Sebastian Meyer s.meyer at technology-network.de
Wed Mar 17 04:26:45 EST 2004


Hi Newsgroup,

i have some problems with using threads and signals in one program.
In my program i have three threads running, one for checking a directory
at a specified interval to see if new data arrived, one waiting for work
and the main thread.

My problem is the following:
I want to shutdown the program via a signal, so i used the signal module.
I know, that signal handling mus be done by the main thread, the signal
handler is installed by the main thread. Now, when both threads are
running and i send the signal to the main thread at my xterm via 
kill -SIGUSR1 PID, it seems that the main program ignores it or does
not execute the signal handler. When i dont use threads, the signal
handler functions properly.

I ll post some code, maybe this is more explainig:

--- CODE ---
import threading
import os,signal

# *** signal handler ***
def shutdown(signum, arg):
    global threads
    
    for th in threads:
        # the shutdown method sets an internal 
        # flag in the derived thread class to 
        # notify the thread to shutdown
        # (implemented using threading.Event)
        th.shutdown()

# *** main ***

# install the signal handler
signal.signal(signal.SIGUSR1, shutdown)

# both classes, TimedDirectoryCheck and BatchProcessor are derived from
# threading.Thread
threads = {}
threads["dircheck"] = TimedDirectoryCheck("/tmp/dpf/notify")
threads["process"] = BatchProcessor()

# start the threads
for th in threads:
    th.start()

--- CODE ---

thats is ... now both threads are running and doing their work. When
i now want to shutdown and send the signal, nothing happens, even
the signal handler will not be called?

Have i missed something? I ve red a lot about threads and signals, 
seems that it sometimes causes strange program behavior...

Anyone any idea what i did wrong?? Thanks for your help in advance..

Sebastian
 



More information about the Python-list mailing list