【发布时间】:2017-06-29 18:01:13
【问题描述】:
我已经提到过这个帖子,但它似乎已经过时了 而且似乎没有明确的解释
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)
限制器线程似乎并没有结束,尽管它是一个守护线程。
这是一种让 IDLE 或 Spyder 工作的方法吗?
谢谢。
【问题讨论】:
-
是什么让你觉得它没有结束?
-
主线程结束后继续输出打印语句('waiting')。这是在使用 Python 控制台(例如 IDLE 或 Spyder)时(不是命令提示符)
标签: python multithreading