欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Python線程協(xié)作threading.Condition如何實(shí)現(xiàn)-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)Python線程協(xié)作threading.Condition如何實(shí)現(xiàn),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)建站是一家專注于成都做網(wǎng)站、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),貢井網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:貢井等地區(qū)。貢井做網(wǎng)站價(jià)格咨詢:18980820575

領(lǐng)會(huì)下面這個(gè)示例吧,其實(shí)跟java中wait/nofity是一樣一樣的道理

import threading


# 條件變量,用于復(fù)雜的線程間同步鎖
"""
需求:
  男:小姐姐,你好呀!
  女:哼,想泡老娘不成?
  男:對呀,想泡你
  女:滾蛋,門都沒有!
  男:切,長這么丑, 還這么吊...
  女:關(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的底層實(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() # 這是釋放__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é)束
   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, 放到一個(gè)deque中,然后釋放掉with鎖, notify時(shí),從deque取拿出鎖,release

python是什么意思

Python是一種跨平臺的、具有解釋性、編譯性、互動(dòng)性和面向?qū)ο蟮哪_本語言,其最初的設(shè)計(jì)是用于編寫自動(dòng)化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨(dú)立的項(xiàng)目和大型項(xiàng)目。

關(guān)于“Python線程協(xié)作threading.Condition如何實(shí)現(xiàn)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

分享文章:Python線程協(xié)作threading.Condition如何實(shí)現(xiàn)-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://chinadenli.net/article20/jgjjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、手機(jī)網(wǎng)站建設(shè)自適應(yīng)網(wǎng)站、建站公司、關(guān)鍵詞優(yōu)化、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
日本欧美一区二区三区在线播| 91欧美一区二区三区| 91亚洲国产日韩在线| 91国自产精品中文字幕亚洲| 欧美韩日在线观看一区| 欧美一区二区三区喷汁尤物| 丁香六月啪啪激情综合区| 日韩亚洲精品国产第二页| 亚洲第一区欧美日韩在线| 亚洲精品有码中文字幕在线观看| 国产一区二区三区不卡| 中文精品人妻一区二区| 国产成人精品久久二区二区| 欧美日韩黑人免费观看| 亚洲人午夜精品射精日韩| 一区二区三区四区亚洲专区| 麻豆剧果冻传媒一二三区| 日本在线 一区 二区| 久久精品中文扫妇内射| 国产一区麻豆水好多高潮| 久久黄片免费播放大全| 国产成人精品在线播放| 邻居人妻人公侵犯人妻视频| 色一欲一性一乱—区二区三区| 不卡视频免费一区二区三区| 亚洲一区二区三区免费的视频| 粉嫩一区二区三区粉嫩视频| 99精品人妻少妇一区二区人人妻| 国产日韩熟女中文字幕| 国产精品午夜视频免费观看| 中日韩美女黄色一级片| 香蕉网尹人综合在线观看| 色婷婷在线精品国自产拍| 国产偷拍精品在线视频| 国产免费黄片一区二区| 正在播放玩弄漂亮少妇高潮| 日本高清一道一二三区四五区| 久久91精品国产亚洲| 国产美女网红精品演绎| 国产乱淫av一区二区三区| 日韩精品你懂的在线观看|