为什么这个这个程序运行后手动结束,打开文件发现什么都没有写入
import queue
import threading
def test_function(a,b):
while True:
p = a.get()
print(p)
b.write('测试内容{}\n'.format(p))
cons=queue.Queue()
b=open('test.txt','w',encoding='utf-8')
for i in range(10):
cons.put(i)
for i in range(2):
t=threading.Thread(target=test_function,args=(cons,b),name='测试线程{}'.format(i))
print(f'开始线程{i}')
t.start()
运行结果如下:
开始线程0
0开始线程1
1
2
3
45
6
7
8
9