国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Python線程協作threading.Condition實現過程解析

瀏覽:9日期:2022-08-02 15:11:36

領會下面這個示例吧,其實跟java中wait/nofity是一樣一樣的道理

import threading# 條件變量,用于復雜的線程間同步鎖'''需求: 男:小姐姐,你好呀! 女:哼,想泡老娘不成? 男:對呀,想泡你 女:滾蛋,門都沒有! 男:切,長這么丑, 還這么吊... 女:關你鳥事!'''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('{}:對呀,想泡你'.format(self.name)) self.condition.wait() self.condition.notify() print('{}:切,長這么丑, 還這么吊...'.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('{}:滾蛋,門都沒有!'.format(self.name)) self.condition.notify() self.condition.wait() print('{}:關你鳥事!'.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的底層實現了__enter__和 __exit__協議.所以可以使用with上下文管理器

由Condition的__init__方法可知,它的底層也是維護了一個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是如何操作的,還是有點懵.....

wait()方法源碼中這樣三行代碼

waiter = _allocate_lock() #從底層獲取了一把鎖,并非Lock鎖waiter.acquire()self._waiters.append(waiter) # 然后將這個鎖加入到_waiters(deque)中saved_state = self._release_save() # 這是釋放__enter__時的那把鎖???

notify()方法源碼

all_waiters = self._waiters waiters_to_notify = _deque(_islice(all_waiters, n))# 從_waiters中取出n個if not waiters_to_notify: # 如果是None,結束 returnfor waiter in waiters_to_notify: # 循環release waiter.release() try: all_waiters.remove(waiter) #從_waiters中移除 except ValueError: pass

大體意思: wait先從底層創建鎖,acquire, 放到一個deque中,然后釋放掉with鎖, notify時,從deque取拿出鎖,release

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 美女精品永久福利在线 | 国产性生活视频 | 欧美三级三级三级爽爽爽 | 美女视频一区二区三区在线 | 亚洲精品久久一区二区无卡 | 亚洲国产2017男人a天堂 | 9999久久| 成人精品视频一区二区在线 | 欧美黄色特级视频 | 在线精品日韩一区二区三区 | 性欧美视频a毛片在线播放 性欧美一级 | 好看的看黄a大片爽爽影院 好男人天堂网 | 国产美女主播一级成人毛片 | 一级片免费观看视频 | 亚洲视频 欧美视频 | 怡红院日本一道日本久久 | 丰满寡妇一级毛片 | japanesevideo乱子 japanese日本tube色系 | 日韩午夜三级 | 成人毛片一区二区三区 | 久久久久国产免费 | 国产精品免费一级在线观看 | 免费观看a黄一级视频 | 91久久香蕉国产线看观看软件 | 国产欧美日韩成人 | 99久久精品免费看国产免费软件 | 9191精品国产免费不久久 | 久久精品在现线观看免费15 | 九九久久国产精品 | 中文字幕水野优香在线网在线 | 亚洲欧美久久一区二区 | 中文字幕一区二区精品区 | 亚洲一级高清在线中文字幕 | 久视频免费精品6 | 亚洲天堂视频网站 | 精品欧美成人bd高清在线观看 | 色综合久久88中文字幕 | 欧美一级片手机在线观看 | a级男女性高爱潮高清试 | 日韩欧美在线视频一区二区 | 国产成人精品日本亚洲专区6 |