python中的守护程序线程与非守护程序线程

时间:2020-01-05 15:54:13

标签: python multithreading daemon

我有以下代码:

import threading 
from time import sleep

def print_function1():
    while True:
        print("Hi this is function 1\n") 
        sleep(2)

if __name__ == "__main__": 
    # creating thread 
    t1 = threading.Thread(target=print_function1 ) 

    t1.daemon = True

    # starting thread 1 
    t1.start() 

    sleep(10)

    # both threads completely executed 
    print("Done!") 

现在,我无法理解如果我将设置为t1.daemon True或False,这会在蜘蛛Ipython控制台中运行代码有什么区别。

在两种情况下程序似乎都没有退出,它一直打印“嗨,这是功能1”。 我的假设是守护程序线程将在主线程完成后继续运行,但普通线程将退出。

任何人都可以解释一下。

1 个答案:

答案 0 :(得分:0)

它使线程在后台运行而不中断主要工作而为真或在假时作为主线程运行