A Question About Handling SIGINT With Threads

K

Keith

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top