Python 2.7: Child thread not catching KeyboardInterrupt
问题 import sys import time import threading class exThread(threading.Thread): def __init__(self, threadId): threading.Thread.__init__(self) self.threadId = threadId def run(self): try: while 1: pass except KeyboardInterrupt: print "Ctrl-C caught by thread" finally: print "Thread's finally clause being executed" sys.exit() # Same as thread.exit() cond = True def func(): pass try: th = exThread(1) th.start() while True: if cond: func() except KeyboardInterrupt: print "Ctrl-C caught by main thread"