tap*_*per 12 python multithreading exception
当一个异常在一个线程内引发但没有在其他任何地方捕获它时,它会杀死整个应用程序/解释器/进程吗?或者它只会杀死线程?
Bo *_*ich 12
我们来试试吧:
import threading
import time
class ThreadWorker(threading.Thread):
def run(self):
print "Statement from a thread!"
raise Dead
class Main:
def __init__(self):
print "initializing the thread"
t = ThreadWorker()
t.start()
time.sleep(2)
print "Did it work?"
class Dead(Exception): pass
Main()
Run Code Online (Sandbox Code Playgroud)
上面的代码产生以下结果:
Run Code Online (Sandbox Code Playgroud)> initializing the thread > Statement from a thread! > Exception in thread > Thread-1: Traceback (most recent call last): File > "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner > self.run() File ".\pythreading.py", line 8, in run > raise Dead Dead > ----- here the interpreter sleeps for 2 seconds ----- > Did it work?
所以,你的问题的答案是,一个引发的Exception只会崩溃它所在的线程,而不是整个程序.