Cleanly exiting multi thread application on SIGINT

J

jrpfinch

Is the following the most elegant way to exit a multi-threaded
application on a Ctrl-C? I am a complete beginner and would have
thought there was some way of doing it without having to use while 1:
pass, but have yet to find a way.

N.B. exit() is a method for cleanly exiting the thread using a queue.

Many thanks

Jon

def main:
wt = workerThread()
wt.setDaemon(True)
wt.start()
ct = counterThread()
ct.setDaemon(True)
ct.start()
try:
while 1:
pass
except KeyboardInterrupt:
wt.exit()
ct.exit()
 
S

skip

Jon> Is the following the most elegant way to exit a multi-threaded
Jon> application on a Ctrl-C? I am a complete beginner and would have
Jon> thought there was some way of doing it without having to use while
Jon> 1: pass, but have yet to find a way.

I thought there was some sort of wait() method in the threading module, but
I was mistaken. Maybe:

while threading.activeCount():
time.sleep(0.01)

?

Skip
 
F

Fredrik Lundh

jrpfinch said:
Is the following the most elegant way to exit a multi-threaded
application on a Ctrl-C? I am a complete beginner and would have
thought there was some way of doing it without having to use while 1:
pass, but have yet to find a way.
def main:
wt = workerThread()
wt.setDaemon(True)
wt.start()
ct = counterThread()
ct.setDaemon(True)
ct.start()
try:
while 1:
pass

you could at least insert a time.sleep(1), to avoid busy-looping.

</F>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top