day46- daemon thread

# 1, guardian of the pit thread to note: The following code can only print out the sub thread starts, you can not print out the child thread is finished, because the main thread in t.start after () is over, 
# and the child thread to sleep one second , so the child thread thread guard with the end of the main thread and over. 
from Threading Import the Thread
 Import Time
 DEF FUNC ():
     Print ( ' child thread start ' ) 
    the time.sleep ( . 1 )
     Print ( ' child thread is finished ' ) 

T = the Thread (target = FUNC) 
t.setDaemon (True) 
T. start () 
# child thread start 

# 2, guard pit thread to note: on the Join: 
from threading Import the thread
 ImportTime
 DEF FUNC ():
     Print ( ' child thread start ' ) 
    the time.sleep ( . 1 )
     Print ( ' child thread is finished ' ) 

T = the Thread (target = FUNC) 
t.setDaemon (True) 
t.start () 
T1 = the thread (target = FUNC) 
t1.start () 
t1.join () # main thread wait for the end of the thread and the end of the sub-t1, t daemon thread wait for the end of the main thread ends. 
# Child thread start 
# child thread start 
# child thread is finished 
# child thread is finished 

# 3, a daemon thread: thread guard With the end of the main thread ends. 
from Threading ImportThe Thread
 DEF FUNC ():
     the while True:
         Print ( ' & ' ) 

T = the Thread (target = FUNC) 
t.setDaemon (True) # daemon thread in front of the start of write. 
t.start ()
 for I in Range (10 ):
     Print ( ' * ' * I)

Guess you like

Origin www.cnblogs.com/python-daxiong/p/12142775.html