-
-
Save kevinastone/a95668df254af2451fb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ?