By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

Problem with threads and signals

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
>
Anyone any idea what i did wrong?? Thanks for your help in advance..

Sebastian


Under Linux, similar things worked for me. Did you call setDaemon(True) on
your bg threads?
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
"Diez B. Roggisch" <de*********@web.de> schrieb im Newsbeitrag
news:c3*************@news.t-online.com...
Under Linux, similar things worked for me. Did you call setDaemon(True) on
your bg threads?

No, i didnt call setDaemon ... is this essential to
gain the functionality i want?

Sebastian
Jul 18 '05 #3

P: n/a
The program below works fine on my system (redhat 9)

A few months ago, debian-specific threading problems in Python were
discussed. If you're using Debian, maybe it's relevant:
http://groups.google.com/groups?thre...ing.google.com

Jeff

import threading, os, signal, time

_shutdown = 0

class T(threading.Thread):
def run(self):
while not _shutdown:
time.sleep(1)

def shutdown(signum, arg):
global _shutdown
print "Shutting down..."
_shutdown = 1

def main():
print "PID:", os.getpid()
signal.signal(signal.SIGUSR1, shutdown)

threads = [T(), T()]

for th in threads:
th.start()

while not _shutdown:
time.sleep(60)

if __name__ == '__main__': main()

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.