0
我有一个Python脚本从另一个线程启动线程。这是一个学习练习,以实际实现kill a python thread的能力,它在我正在编写的应用程序中有用处。忽略红鲱鱼,线程FirstThread
启动线程SecondThread
,这是我们的目的被陷入循环,并没有资源释放。考虑:为什么daemon线程在其环境被破坏时不会死亡?
import threading
import time
class FirstThread (threading.Thread):
def run(self):
b = SecondThread()
b.daemon = True
b.start()
time.sleep(3)
print("FirstThread going away!")
return True
class SecondThread (threading.Thread):
def run(self):
while True:
time.sleep(1)
print("SecondThread")
a = FirstThread()
a.daemon = True
a.start()
print("Waiting 5 seconds.")
time.sleep(5)
print("Done waiting")
虽然FirstThread
确实打印出“FirstThread离开!”如预期的3秒后,SecondThread
继续打印“SecondThread”到stdout。我预计SecondThread
被销毁,FirstThread
被销毁,因为它是a daemon thread。那么为什么SecondThread
即使其环境(FirstThread)已被破坏,仍然存在?