1
我已经提到了这个线程,但它似乎已经过时 和似乎没有成为一个干净的解释守护线程不退出,尽管主程序完成
Python daemon thread does not exit when parent thread exits
我运行python 3.6并尝试从IDLE或Spyder IDE运行脚本。
这里是我的代码:
import threading
import time
total = 4
def creates_items():
global total
for i in range(10):
time.sleep(2)
print('added item')
total += 1
print('creation is done')
def creates_items_2():
global total
for i in range(7):
time.sleep(1)
print('added item')
total += 1
print('creation is done')
def limits_items():
#print('finished sleeping')
global total
while True:
if total > 5:
print ('overload')
total -= 3
print('subtracted 3')
else:
time.sleep(1)
print('waiting')
limitor = threading.Thread(target = limits_items, daemon = True)
creator1 = threading.Thread(target = creates_items)
creator2 = threading.Thread(target = creates_items_2)
print(limitor.isDaemon())
creator1.start()
creator2.start()
limitor.start()
creator1.join()
creator2.join()
print('our ending value of total is' , total)
limitor线程似乎并不尽管是一个守护线程将被终止。
这是一种让IDLE或Spyder工作的方法吗?
谢谢。
是什么让你觉得它并没有结束? –
主线程结束后,print语句('waiting')继续输出。这是使用Python控制台,如IDLE或Spyder(不是命令提示符) – Moondra