Python線程協(xié)作threading.Condition實(shí)現(xiàn)過(guò)程解析
領(lǐng)會(huì)下面這個(gè)示例吧,其實(shí)跟java中wait/nofity是一樣一樣的道理
import threading# 條件變量,用于復(fù)雜的線程間同步鎖'''需求: 男:小姐姐,你好呀! 女:哼,想泡老娘不成? 男:對(duì)呀,想泡你 女:滾蛋,門都沒(méi)有! 男:切,長(zhǎng)這么丑, 還這么吊... 女:關(guān)你鳥事!'''class Boy(threading.Thread): def __init__(self, name, condition): super().__init__(name=name) self.condition = condition def run(self): with self.condition: print('{}:小姐姐,你好呀!'.format(self.name)) self.condition.wait() self.condition.notify() print('{}:對(duì)呀,想泡你'.format(self.name)) self.condition.wait() self.condition.notify() print('{}:切,長(zhǎng)這么丑, 還這么吊...'.format(self.name)) self.condition.wait() self.condition.notify()class Girl(threading.Thread): def __init__(self, name, condition): super().__init__(name=name) self.condition = condition def run(self): with self.condition: print('{}:哼,想泡老娘不成?'.format(self.name)) self.condition.notify() self.condition.wait() print('{}:滾蛋,門都沒(méi)有!'.format(self.name)) self.condition.notify() self.condition.wait() print('{}:關(guān)你鳥事!'.format(self.name)) self.condition.notify() self.condition.wait()if __name__ == ’__main__’: condition = threading.Condition() boy_thread = Boy(’男’, condition) girl_thread = Girl(’女’, condition) boy_thread.start() girl_thread.start()
Condition的底層實(shí)現(xiàn)了__enter__和 __exit__協(xié)議.所以可以使用with上下文管理器
由Condition的__init__方法可知,它的底層也是維護(hù)了一個(gè)RLock鎖
def __enter__(self): return self._lock.__enter__()
def __exit__(self, *args): return self._lock.__exit__(*args)
def __exit__(self, t, v, tb): self.release()
def release(self): '''Release a lock, decrementing the recursion level. If after the decrement it is zero, reset the lock to unlocked (not owned by any thread), and if any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed. If after the decrement the recursion level is still nonzero, the lock remains locked and owned by the calling thread. Only call this method when the calling thread owns the lock. A RuntimeError is raised if this method is called when the lock is unlocked. There is no return value. ''' if self._owner != get_ident(): raise RuntimeError('cannot release un-acquired lock') self._count = count = self._count - 1 if not count: self._owner = None self._block.release()
至于wait/notify是如何操作的,還是有點(diǎn)懵.....
wait()方法源碼中這樣三行代碼
waiter = _allocate_lock() #從底層獲取了一把鎖,并非Lock鎖waiter.acquire()self._waiters.append(waiter) # 然后將這個(gè)鎖加入到_waiters(deque)中saved_state = self._release_save() # 這是釋放_(tái)_enter__時(shí)的那把鎖???
notify()方法源碼
all_waiters = self._waiters waiters_to_notify = _deque(_islice(all_waiters, n))# 從_waiters中取出n個(gè)if not waiters_to_notify: # 如果是None,結(jié)束 returnfor waiter in waiters_to_notify: # 循環(huán)release waiter.release() try: all_waiters.remove(waiter) #從_waiters中移除 except ValueError: pass
大體意思: wait先從底層創(chuàng)建鎖,acquire, 放到一個(gè)deque中,然后釋放掉with鎖, notify時(shí),從deque取拿出鎖,release
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?2. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼3. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁(yè)4. asp知識(shí)整理筆記4(問(wèn)答模式)5. 小技巧處理div內(nèi)容溢出6. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法7. ASP實(shí)現(xiàn)加法驗(yàn)證碼8. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)9. js的一些潛在規(guī)則使用分析10. phpstudy apache開(kāi)啟ssi使用詳解
