麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Python > 正文

Python線程同步的實現代碼

2020-02-15 23:05:46
字體:
來源:轉載
供稿:網友

本文介紹Python中的線程同步對象,主要涉及 thread 和 threading 模塊。

threading 模塊提供的線程同步原語包括:Lock、RLock、Condition、Event、Semaphore等對象。

線程執行

join與setDaemon

子線程在主線程運行結束后,會繼續執行完,如果給子線程設置為守護線程(setDaemon=True),主線程運行結束子線程即結束;

如果join()線程,那么主線程會等待子線程執行完再執行。

import threadingimport timedef get_thread_a(): print("get thread A started") time.sleep(3) print("get thread A end")def get_thread_b(): print("get thread B started") time.sleep(5) print("get thread B end")if __name__ == "__main__": thread_a = threading.Thread(target=get_thread_a) thread_b = threading.Thread(target=get_thread_b) start_time = time.time() thread_b.setDaemon(True) thread_a.start() thread_b.start() thread_a.join()  end_time = time.time() print("execution time: {}".format(end_time - start_time))

thread_a是join,首先子線程thread_a執行,thread_b是守護線程,當主線程執行完后,thread_b不會再執行執行結果如下:

get thread A started
get thread B started
get thread A end
execution time: 3.003199815750122

線程同步

當線程間共享全局變量,多個線程對該變量執行不同的操作時,該變量最終的結果可能是不確定的(每次線程執行后的結果不同),如:對count變量執行加減操作 ,count的值是不確定的,要想count的值是一個確定的需對線程執行的代碼段加鎖。

python對線程加鎖主要有Lock和Rlock模塊

Lock: 

from threading import Locklock = Lock()lock.acquire()lock.release() 

Lock有acquire()和release()方法,這兩個方法必須是成對出現的,acquire()后面必須release()后才能再acquire(),否則會造成死鎖

Rlock:

鑒于Lock可能會造成死鎖的情況,RLock(可重入鎖)對Lock進行了改進,RLock可以在同一個線程里面連續調用多次acquire(),但必須再執行相同次數的release()

from threading import RLocklock = RLock()lock.acquire()lock.acquire()lock.release()lock.release() 

condition(條件變量),線程在執行時,當滿足了特定的條件后,才可以訪問相關的數據

import threadingdef get_thread_a(condition): with condition:  condition.wait()  print("A : Hello B,that's ok")  condition.notify()  condition.wait()  print("A : I'm fine,and you?")  condition.notify()  condition.wait()  print("A : Nice to meet you")  condition.notify()  condition.wait()  print("A : That's all for today")  condition.notify()def get_thread_b(condition): with condition:  print("B : Hi A, Let's start the conversation")  condition.notify()  condition.wait()  print("B : How are you")  condition.notify()  condition.wait()  print("B : I'm fine too")  condition.notify()  condition.wait()  print("B : Nice to meet you,too")  condition.notify()  condition.wait()  print("B : Oh,goodbye")if __name__ == "__main__": condition = threading.Condition() thread_a = threading.Thread(target=get_thread_a, args=(condition,)) thread_b = threading.Thread(target=get_thread_b, args=(condition,)) thread_a.start() thread_b.start()             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: av资源在线天堂 | 久久精品国产精品亚洲 | 久久久一区二区 | 日韩视频一二区 | 精精国产xxxx视频在线野外 | 亚洲少妇诱惑 | chinesexxxx刘婷hd 国产91在线播放九色 | 日韩精品中文字幕一区二区三区 | 欧美日韩手机在线观看 | 色网站免费观看 | 二区三区四区视频 | 久久亚洲成人 | 精品久久久久久久久久久αⅴ | 黄色网址在线视频 | 中文字幕网址 | 国产精品一区二区手机在线观看 | 精品二区在线观看 | 99ri在线 | 激情久久一区二区 | 精品一区二区三区日本 | 国产一级毛片在线看 | 欧美亚州 | 一区二区三区日韩 | 精品一区二区久久久久 | 热99re久久免费视精品频软件 | 日韩av在线播放一区 | 极品一级片 | xxxxhd18hd日本hd| av在线免费播放网站 | 91九色论坛| 国产精品久久久网站 | 国产精品久久久久久婷婷天堂 | 亚洲极色| 精品一区二区三区日本 | 九九热在线视频观看 | 九九黄色 | 精品免费国产一区二区三区 | 精品一区二区三区在线视频 | 成人男女激情免费视频 | 深夜小视频在线观看 | 国产黄色网 |