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

多線程python實現和多線程有序性

編輯:Python

文章目錄

  • 前言
  • 一、多線程運行無序問題
  • 二、“join方法”解決多線程運行無序問題
  • 三、threading.Thread()的常用參數
  • 總結


前言

多線程一般用於同時調用多個函數,cpu時間片輪流分配給多個任務。 優點是提高cpu的使用率,使計算機減少處理多個任務的總時間;缺點是如果有全局變量,調用多個函數會使全局變量被多個函數修改,造成計算錯誤,這使需要使用join方法或者設置局部變量來解決問題。python使用threading模塊來實現多線程threading.join()方法是保證調用join的子線程完成後,才會分配cpu給其他的子線程,從而保證線程運行的有序性


一、多線程運行無序問題

我們首先創建三個實例,t1,t2,t3 t1實例調用function1函數,t2和t3函數調用function11函數,他們都是對全局變量l1進行操作
代碼如下,

import threading,time
l1 = []
#創建RLock鎖,acquire幾次,release幾次
lock = threading.RLock()
def function1(x,y):
for i in range(x):
l1.append(i)
if i == 0:
time.sleep(1)
end_time = time.time()
print("t{} is finished in {}s".format(y,end_time -time1 ))
def function11(x,y):
for i in range(x):
l1.append(i)
end_time = time.time()
print("t{} is finished in {}s".format(y, end_time -time1))
#2.創建子線程:thread類
if __name__ == '__main__':
t1 = threading.Thread(target= function1, args = (100,1))
t2 = threading.Thread(target= function11, args = (100,2))
t3 = threading.Thread(target= function11, args = (100,3))
time1 = time.time()
print("time starts in {}".format(time1))
t1.start()
t2.start()
t3.start()
print(l1)

結果如下,

runfile('E:/桌面/temp.py', wdir='E:/桌面')
time starts in 1656474963.9487
t2 is finished in 0.0s
t3 is finished in 0.0s
[0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
t1 is finished in 1.0152690410614014s

我們可以看到,全局變量中開頭有兩個0,而不是按著0,1,2,3的方式按序填充,所以可以得知全局變量在多線程中是被多個函數無序調用的。為了保證多線程有序調用全局變量,我們可以利用threading.join()的方法。

二、“join方法”解決多線程運行無序問題

我們重寫了function1函數,並命名為function2,t1調用function2函數。t2,t3不變。
代碼如下,

import threading,time
l1 = []
#創建RLock鎖,acquire幾次,release幾次
lock = threading.RLock()
def function1(x,y):
for i in range(x):
l1.append(i)
if i == 0:
time.sleep(1)
end_time = time.time()
print("t{} is finished in {}s".format(y,end_time -time1))
def function11(x,y):
for i in range(x):
l1.append(i)
end_time = time.time()
print("t{} is finished in {}s".format(y,end_time -time1))
def function2(x,y):
for i in range(x):
l1.append(i)
if i == 0:
time.sleep(1)
end_time = time.time()
print("t{} is finished in {}s".format(y,end_time -time1))
#2.創建子線程:thread類
if __name__ == '__main__':
t1 = threading.Thread(target= function2, args = (100,1))
t2 = threading.Thread(target= function11, args = (100,2))
t3 = threading.Thread(target= function11, args = (100,3))
time1 = time.time()
print("time starts in {}".format(time1))
t1.start()
t1.join()
t2.start()
t3.start()
print(l1)

結果如下,

runfile('E:/桌面/temp.py', wdir='E:/桌面')
time starts in 1656476057.441827
t1 is finished in 1.0155227184295654s
t2 is finished in 1.0155227184295654s
t3 is finished in 1.0155227184295654s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

由此可見,threading.join()方法可以解決多線程無序問題

三、threading.Thread()的常用參數

1.group:默認值None,為了實現ThreadGroup類而保留
2.target:在start方法中調用的可調用對象,即需要開啟線程的可調用對象,比如函數、方法
3.name:默認為“Thread-N”,字符串形式的線程名稱
4.args:默認為空元組,參數target中傳入的可調用對象的參數元組
5.kwargs:默認為空字典{},參數target中傳入的可調用對象的關鍵字參數字典
6.daemon:默認為None

總結

以上就是python多線程常用參數和多線程有序性的介紹,後續還有更深入的多線程文章請持續關注。


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