One 、setDaemon(False) When a process starts , A main thread will be generated by default , Because threads are the smallest unit of program execution , When setting up multithreading , The main thread creates multiple sub threads , stay python in , By default setDemon(False), After the main thread finishes executing its own tasks , Dropped out , At this point, the sub thread will continue to perform its own tasks , Know your mission is over .
import threading
import time
def thread():
for i in range(5):
print(f' Sub thread {
i} Start execution ')
time.sleep(2)
print(f' Sub thread {
i} End to perform ')
if __name__ == '__main__':
t=threading.Thread(target=thread)
t.start()
print(' Main thread execution completed ')
Execution results :
Sub thread 0 Start executing the main thread and finish executing
Sub thread 0 End to perform
Sub thread 1 Start execution
Sub thread 1 End to perform
Sub thread 2 Start execution
Sub thread 2 End to perform
Sub thread 3 Start execution
Sub thread 3 End to perform
Sub thread 4 Start execution
Sub thread 4 End to perform
Two 、setDaemon(True) When we use setDaemon(True) when , This is when the child thread is a daemon thread , Once the main thread finishes executing , All child threads are forcibly terminated
import threading
import time
def thread():
for i in range(5):
print(f' Sub thread {
i} Start execution ')
time.sleep(2)
print(f' Sub thread {
i} End to perform ')
if __name__ == '__main__':
t=threading.Thread(target=thread)
t.setDaemon(True)
t.start()
print(' Main thread execution completed ')
Execution results :
Sub thread 0 Start executing the main thread and finish executing
3、 ... and 、 join( Thread synchronization ): join All that's done is thread synchronization , That is, after the main thread task ends , Into a state of blockage , Wait until all the child threads are finished , The main thread terminates again .
If not set timeout Parameter waits for the child thread to end before the main thread ends
import threading
import time
def thread():
for i in range(5):
print(f' Sub thread {
i} Start execution ')
time.sleep(2)
print(f' Sub thread {
i} End to perform ')
if __name__ == '__main__':
t=threading.Thread(target=thread)
t.start()
t.join()
print(' Main thread execution completed ')
Execution results :
Sub thread 0 Start execution
Sub thread 0 End to perform
Sub thread 1 Start execution
Sub thread 1 End to perform
Sub thread 2 Start execution
Sub thread 2 End to perform
Sub thread 3 Start execution
Sub thread 3 End to perform
Sub thread 4 Start execution
Sub thread 4 End to perform
Main thread execution completed
import threading
import time
def thread():
for i in range(5):
print(f' Sub thread {
i} Start execution ')
time.sleep(2)
print(f' Sub thread {
i} End to perform ')
if __name__ == '__main__':
t=threading.Thread(target=thread)
t.start()
t.join(timeout=3)
print(' Main thread execution completed ')
Execution results : Main thread blocked 3s after , The main thread ends execution , The child thread continues to execute .
Sub thread 0 Start execution
Sub thread 0 End to perform
Sub thread 1 Start execution
Main thread execution completed
Sub thread 1 End to perform
Sub thread 2 Start execution
Sub thread 2 End to perform
Sub thread 3 Start execution
Sub thread 3 End to perform
Sub thread 4 Start execution
Sub thread 4 End to perform
import threading
import time
def thread():
for i in range(5):
print(f' Sub thread {
i} Start execution ')
time.sleep(2)
print(f' Sub thread {
i} End to perform ')
if __name__ == '__main__':
t=threading.Thread(target=thread)
t.setDaemon(True)
t.start()
t.join(timeout=3)
print(' Main thread execution completed ')
Execution results : Main thread blocked 3s after , Main thread end task , The child thread also forces the end of the task
Sub thread 0 Start execution
Sub thread 0 End to perform
Sub thread 1 Start execution
Main thread execution completed