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

A Question About Handling SIGINT With Threads

P: n/a
I got this to work, but was hoping there was a better way. I was
hoping that I wouldn't have to use globals. I wouldn't doubt that I
could be totally off in what I am doing.

Here is some psuedo-code of what I did:

def main_thread():
# Don't know how to get rid of these guys
global lock
global my_thread

# Set up a signal handler for Ctrl-C
signal.signal(signal.SIGINT, sighdlr)

# Create & start thread
lock = thread.allocate_lock()
lock.acquire()
my_thread = threading.Thread(
target=child_thread,
name='thread_test',
args=(lock,)
my_thread.start()

do_some_work_here()

# Tell thread to stop working
lock.release()

# Wait for child thread to finish
my_thread.join()

def child_thread(lock):

while lock.locked():
do_some_stuff()

do_some_cleanup()

def sighdlr(signum, frame):

# Allow child to finish
lock.release()
my_thread.join()
sys.exit(1)

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.