這篇文章主要介紹“python線程通信Condition的實(shí)例用法介紹”,在日常操作中,相信很多人在python線程通信Condition的實(shí)例用法介紹問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”python線程通信Condition的實(shí)例用法介紹”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)長(zhǎng)期為上千多家客戶(hù)提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為楊浦企業(yè)提供專(zhuān)業(yè)的成都網(wǎng)站建設(shè)、成都做網(wǎng)站,楊浦網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
1、acquire調(diào)用Condition關(guān)聯(lián)的方法。
Lock的acquire()或release()。
2、wait傳入timeout參數(shù)。
指定該線程最多等待多少秒。
導(dǎo)致當(dāng)前線程進(jìn)入Condition的等待池等待通知并釋放鎖,直到其他線程調(diào)用該Condition的notify()或者notify_all()方法來(lái)喚醒該線程。在調(diào)用該wait()方法時(shí)可以
3、notify喚醒Condition的單個(gè)線程并通知。
收到通知的線程會(huì)自動(dòng)調(diào)用accquire()方法嘗試加鎖。如果所有線程都在該Condition等待池中等待,則會(huì)選擇喚醒其中一個(gè)線程,選擇是任意性的。
4、notify_all喚醒所有線程并通知。
實(shí)例
import threading class Account: # 定義構(gòu)造函數(shù) def __init__(self, account_no, balance): self.account_no = account_no self._balance = balance self.condition = threading.Condition() # 定義代表是否已經(jīng)存錢(qián)的標(biāo)識(shí) self.__deposit_flag = False # 取錢(qián) def draw(self, draw_amount): # 加鎖 self.condition.acquire() try: # 還沒(méi)存錢(qián) if not self.__deposit_flag: self.condition.wait() else: if self._balance >= draw_amount: self._balance = self._balance - draw_amount print(threading.current_thread().getName() + " 取錢(qián)成功,賬戶(hù)余額是:" + str(self._balance) + "\n") else: print(threading.current_thread().getName() + " 取錢(qián)失敗\n") # 將標(biāo)識(shí)賬戶(hù)已有存款的標(biāo)識(shí)改成False self.__deposit_flag = False # 喚醒其他等待現(xiàn)車(chē)線程 self.condition.notify_all() finally: # 釋放鎖 self.condition.release() # 存錢(qián) def deposit(self, deposit_amount): # 加鎖 self.condition.acquire() try: # 如果已經(jīng)存款了,則等待取款 if self.__deposit_flag: self.condition.wait() else: self._balance = self._balance + deposit_amount print(threading.current_thread().getName() + " 存款成功,存款金額是:" + str(deposit_amount) + "\n") # 將存款標(biāo)識(shí)改成已存款 self.__deposit_flag = True # 喚醒其他線程 self.condition.notify_all() finally: # 釋放鎖 self.condition.release() def draw_many(account, draw_amount, max): for i in range(max): account.draw(draw_amount) def deposit_many(account, deposit_amount, max): for i in range(max): account.deposit(deposit_amount) # 創(chuàng)建一個(gè)賬戶(hù) account = Account("賬戶(hù)一", 0) # 創(chuàng)建并啟動(dòng)取錢(qián)線程 draw_1 = threading.Thread(name='取錢(qián)者一', target=draw_many, args=(account, 200, 50)) draw_1.start() draw_2 = threading.Thread(name='取錢(qián)者二', target=draw_many, args=(account, 200, 50)) draw_2.start() # 創(chuàng)建并啟動(dòng)存錢(qián)線程 deposit_1 = threading.Thread(name='存錢(qián)者一', target=deposit_many, args=(account, 200, 50)) deposit_1.start() deposit_2 = threading.Thread(name='存錢(qián)者二', target=deposit_many, args=(account, 200, 50)) deposit_2.start() draw_1.join() draw_2.join() deposit_1.join() deposit_2.join()
到此,關(guān)于“python線程通信Condition的實(shí)例用法介紹”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
網(wǎng)頁(yè)標(biāo)題:python線程通信Condition的實(shí)例用法介紹
標(biāo)題鏈接:http://bm7419.com/article18/jddcgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、關(guān)鍵詞優(yōu)化、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)