gpt4 book ai didi

python - 线程内未捕获异常是只杀死线程还是整个进程?

转载 作者:太空狗 更新时间:2023-10-29 17:43:30 24 4
gpt4 key购买 nike

当一个线程内部引发异常而没有在其他任何地方捕获它时,它会杀死整个应用程序/解释器/进程吗?还是只会杀死线程?

最佳答案

让我们试试看:

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()

上面的代码产生以下结果:

> 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?

因此,您的问题的答案是引发的异常只会使它所在的线程崩溃,而不是整个程序。

关于python - 线程内未捕获异常是只杀死线程还是整个进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13513627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com