色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

瀏覽:4日期: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 編程
相關文章:
主站蜘蛛池模板: 一区二区三区观看 | 欧美国一级毛片片aa | 激情宗合网 | 亚洲狠狠综合久久 | 高清国产露脸捆绑01经典 | 香蕉99国内自产自拍视频 | 欧美人成人亚洲专区中文字幕 | 国产午夜亚洲精品理论片不卡 | 美国免费三片在线观看 | 国产片一级aaa毛片视频 | 日韩视频国产 | 亚洲天堂在线观看视频 | 美国毛片免费一级 | 成人爽爽大片在线观看 | 精品日韩欧美一区二区三区 | 在线久草视频 | 九九免费精品视频 | se94se最新网站 | 国产区最新| 亚洲国产日韩欧美综合久久 | 久久久99精品免费观看精品 | 中文字幕久久亚洲一区 | 国产成人在线播放视频 | 国产片久久| 亚洲成人免费 | 欧美乱一级在线观看 | 国产欧美一区二区日本加勒比 | 亚洲国产天堂久久综合图区 | 久久精品国产大片免费观看 | 日韩在线播放中文字幕 | 清纯偷拍精品视频在线观看 | 亚洲午夜片 | 午夜爽爽爽男女免费观看hd | 特级a欧美孕妇做爰片毛片 特级a欧美做爰片毛片 | 国产成人久久久精品一区二区三区 | 国产成人精品高清免费 | 久久国产情侣 | 中文字幕在线看视频一区二区三区 | 男女视频免费观看 | 黄色三级视频 | 国产成人国产在线观看入口 |