我在学习多线程执行函数的时候遇到了一个小问题,就是我想在其中一个函数中结束所有指令的运行,但是似乎exit()不起作用,我还试了quit(),sys.exit()也不行
希望能帮我解决,多谢多谢!
from threading import Thread
import time
import sys
def fun1():
print('fun11')
time.sleep(2)
print("fun1")
exit() #我想在这个位置使程序全部终止,但是这条exit()并没有阻止fun2()中的print("fun2")命令运行
def fun2():
print('fin22')
time.sleep(6)
print("fun2")
threads = []
threads.append(Thread(target=fun1))
threads.append(Thread(target=fun2))
if name == 'main':
for t in threads:
t.start()
fun11
fin22
fun1
fun2
[Finished in 6.3s]
我希望的结果应该是这样的
fun11
fin22
fun1
[Finished in 2.3s] #这里应该是2秒多
sys.exit()