程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python interview questions: thread synchronization in Python

編輯:Python

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 .

1、 Example : No addition setDaemon() perhaps setDaemon(False)

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

2、 Example :setDaemon(True)

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

3、 Example :t.join()

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

4、 Example :t.join(timeout=3)

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

5、 Example :t.join(timeout=3) and setDaemon(True)

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved