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(s ignal.SIGUSR1, shutdown)
# both classes, TimedDirectoryC heck and BatchProcessor are derived from
# threading.Threa d
threads = {}
threads["dircheck"] = TimedDirectoryC heck("/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
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(s ignal.SIGUSR1, shutdown)
# both classes, TimedDirectoryC heck and BatchProcessor are derived from
# threading.Threa d
threads = {}
threads["dircheck"] = TimedDirectoryC heck("/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
Comment