我有一些创建恶魔线程的Python代码。父线程几乎立即结束,但守护线程继续打印睡眠。当父线程退出时,Python守护进程线程不会退出
import threading
import time
def int_sleep():
for _ in range(1, 600):
time.sleep(1)
print("sleep")
def main():
thread = threading.Thread(target=int_sleep)
thread.daemon = True
thread.start()
time.sleep(2)
print("main thread end...")
thread = threading.Thread(target=main)
thread.start()
内容sys.version:
'3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:19:30) [MSC v.1600 64 bit (AMD64)]'
打印:
sleep
main thread end...
sleep
sleep
sleep
为什么不Python的守护线程退出父线程退出时?
退房的thread.daemon设置.. http://stackoverflow.com/questions/4330111/python-thread -daemon-property –