最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python線程協(xié)作threading.Condition實現(xiàn)過程解析

 更新時間:2020年03月12日 12:27:29   作者:我太難了008  
這篇文章主要介紹了Python線程協(xié)作threading.Condition實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

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

import threading


# 條件變量,用于復雜的線程間同步鎖
"""
需求:
  男:小姐姐,你好呀!
  女:哼,想泡老娘不成?
  男:對呀,想泡你
  女:滾蛋,門都沒有!
  男:切,長這么丑, 還這么吊...
  女:關(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("{}:對呀,想泡你".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("{}:關(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的底層實現(xiàn)了__enter__和 __exit__協(xié)議.所以可以使用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,結(jié)束
   return
for waiter in waiters_to_notify: # 循環(huán)release
   waiter.release()
   try:
     all_waiters.remove(waiter) #從_waiters中移除
   except ValueError:
     pass

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python使用ThreadPoolExecutor一次開啟多個線程

    Python使用ThreadPoolExecutor一次開啟多個線程

    通過使用ThreadPoolExecutor,您可以同時開啟多個線程,從而提高程序的并發(fā)性能,本文就來介紹一下Python使用ThreadPoolExecutor一次開啟多個線程,感興趣的可以了解一下
    2023-11-11
  • web.py 十分鐘創(chuàng)建簡易博客實現(xiàn)代碼

    web.py 十分鐘創(chuàng)建簡易博客實現(xiàn)代碼

    web.py是一款輕量級的Python web開發(fā)框架,簡單、高效、學習成本低,特別適合作為python web開發(fā)的入門框架
    2016-04-04
  • Python常見MongoDB數(shù)據(jù)庫操作實例總結(jié)

    Python常見MongoDB數(shù)據(jù)庫操作實例總結(jié)

    這篇文章主要介紹了Python常見MongoDB數(shù)據(jù)庫操作,結(jié)合實例形式詳細總結(jié)了Python針對MongoDB數(shù)據(jù)庫相關(guān)pymongo庫安裝以及MongoDB數(shù)據(jù)庫的增刪改查等相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2018-07-07
  • nx.adjacency_matrix計算鄰接矩陣與真實結(jié)果不一致的解決

    nx.adjacency_matrix計算鄰接矩陣與真實結(jié)果不一致的解決

    這篇文章主要介紹了nx.adjacency_matrix計算鄰接矩陣與真實結(jié)果不一致的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Python遍歷指定文件夾下的所有文件名的方法小結(jié)

    Python遍歷指定文件夾下的所有文件名的方法小結(jié)

    當需要遍歷指定文件夾下的所有文件名時,Python提供了多種方法來實現(xiàn)這個任務,本文將介紹如何使用Python來完成這一任務,有需要的小伙伴可以參考下
    2024-01-01
  • python統(tǒng)計mysql數(shù)據(jù)量變化并調(diào)用接口告警的示例代碼

    python統(tǒng)計mysql數(shù)據(jù)量變化并調(diào)用接口告警的示例代碼

    這篇文章主要介紹了python統(tǒng)計mysql數(shù)據(jù)量變化并調(diào)用接口告警的示例代碼,幫助大家更好的利用python操作數(shù)據(jù)庫,感興趣的朋友可以了解下
    2020-09-09
  • 通過實例解析Python return運行原理

    通過實例解析Python return運行原理

    這篇文章主要介紹了通過實例解析Python return運行原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • python 內(nèi)置庫wsgiref的使用(WSGI基礎入門)

    python 內(nèi)置庫wsgiref的使用(WSGI基礎入門)

    WSGI(web服務器網(wǎng)關(guān)接口)主要規(guī)定了服務器端和應用程序之間的接口,即規(guī)定了請求的URL到后臺處理函數(shù)之間的映射該如何實現(xiàn)。wsgiref是一個幫助開發(fā)者開發(fā)測試的Python內(nèi)置庫,程序員可以通過這個庫了解WSGI的基本運行原理,但是不能把它用在生產(chǎn)環(huán)境上。
    2021-06-06
  • python實現(xiàn)AES和RSA加解密的方法

    python實現(xiàn)AES和RSA加解密的方法

    這篇文章主要為大家詳細介紹了python實現(xiàn)AES和RSA加解密的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • python爬取分析超級大樂透歷史開獎數(shù)據(jù)

    python爬取分析超級大樂透歷史開獎數(shù)據(jù)

    這篇文章主要介紹了python爬取分析超級大樂透歷史開獎數(shù)據(jù),本次使用了requests和beautifulsoup庫進行數(shù)據(jù)的爬取,通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02

最新評論

满洲里市| 班戈县| 鄂州市| 蒙城县| 花垣县| 枣阳市| 安福县| 辉南县| 新源县| 合水县| 唐海县| 吉隆县| 安远县| 天镇县| 色达县| 丰县| 彭水| 高要市| 美姑县| 兴和县| 三台县| 五原县| 潮州市| 荥阳市| 温州市| 礼泉县| 太仓市| 祁阳县| 尉氏县| 沐川县| 无极县| 明水县| 惠来县| 高陵县| 鄂伦春自治旗| 胶州市| 永靖县| 西丰县| 上犹县| 香格里拉县| 绥中县|