简体   繁体   中英

Daemon thread vs Non Daemon thread in python

I have following code :

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!") 

Now I am not able in understand what difference does it make if I set , t1.daemon True or False , I am running code in spider Ipython console.

In both cases program don't seems to exit, It keep printing "Hi this is function 1". My assumption was daemon thread will keep running when main thread finishes , but normal thread will exit.

can anyone explain please.

This issue is because different behaviour observed with daemon threads while running python code into python shell , let say Ipython in Spyder in my case vs running python file from command line like " python thread_example.py ".

Running file from command line gives expected behaviour.

One can refer this stackoverflow answer : Python daemon thread does not exit when parent thread exits

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM