Skip to content

Instantly share code, notes, and snippets.

@kevinastone
Created October 29, 2013 08:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinastone/a95668df254af2451fb7 to your computer and use it in GitHub Desktop.
Save kevinastone/a95668df254af2451fb7 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import signal, sys, threading, time
THREADS = []
def handler(signal, frame):
global THREADS
print "Ctrl-C.... Exiting"
for t in THREADS:
t.alive = False
sys.exit(0)
class thread(threading.Thread):
def __init__(self):
self.alive = True
threading.Thread.__init__(self)
def run(self):
while self.alive:
# do something
pass
def main():
global THREADS
t = thread()
t.start()
THREADS.append(t)
for t in THREADS:
while True:
t.join(100000)
if not t.isAlive:
break
# while True:
# time.sleep(1)
print 'exiting'
if __name__ == '__main__':
signal.signal(signal.SIGINT, handler)
main()
@sanfx
Copy link

sanfx commented Jun 21, 2014

if the thread you are setting to alive to False has a child thread which is set Daemon to False will this strategy to exit by CTRL+C work ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment