<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
多執行緒一般用於同時呼叫多個函數,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()的方法。
我們重寫了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()方法可以解決多執行緒無序問題
1.group:預設值None,為了實現ThreadGroup類而保留
2.target:在start方法中呼叫的可呼叫物件,即需要開啟執行緒的可呼叫物件,比如函數、方法
3.name:預設為“Thread-N”,字串形式的執行緒名稱
4.args:預設為空元組,引數target中傳入的可呼叫物件的引數元組
5.kwargs:預設為空字典{},引數target中傳入的可呼叫物件的關鍵字引數字典
6.daemon:預設為None
到此這篇關於多執行緒python的實現及多執行緒有序性的文章就介紹到這了,更多相關python多執行緒內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45