亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Python實現生產者、消費者問題

發布時間:2021-04-09 13:00:16 來源:億速云 閱讀:439 作者:小新 欄目:開發技術

這篇文章主要介紹使用Python實現生產者、消費者問題,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

生產者、消費者問題,經典的線程同步問題:假設有一個緩沖池(列表),生產者往里面放東西,消費者從里面取,規則是:列表為空的時候,生產者才能放東西;列表不為空的時候,消費者才能取東西;為了簡單起見,暫定緩沖池中最多只能有一個產品。這里生產者和消費者共同操作一個資源:緩沖池,因此每次操作的時候,需要給資源加鎖,操作結束時,釋放鎖,這樣才能做到資源同步。使用python實現,需要繼承Thread類,獲取鎖對象,代碼如下:

# -*- coding:utf-8 -*-
#! python2
from threading import Thread
from threading import Lock
import time,random
pro_list = []
lock = Lock()
class Producer(Thread):
  def run(self):
    global pro_list
    while True:
      i = random.randint(0, 100)
      lock.acquire()
      if len(pro_list) > 0:
        print "!--product still in list, wait consumer to get it.."
      else:
        pro_list.append(i)
        print ":::Producer put:", pro_list[0]
      lock.release()
      time.sleep(2)
class Consumer(Thread):
  def run(self):
    global pro_list
    while True:
      lock.acquire()
      if len(pro_list) == 0:
        print "!--No product now, wait producer put in..."
      else:
        print ":::Consumer fetch:", pro_list[0]
        pro_list.pop(0)
      lock.release()
      time.sleep(2)
Producer().start()
Producer().start()
Consumer().start()
Producer().start()
Producer().start()
Consumer().start()
Consumer().start()

這里使用多個生產者和消費者,共同操作緩沖池,部分執行結果如下:

:::Producer put: 78
!--product still in list, wait consumer to get it..
:::Consumer fetch: 78
:::Producer put: 99
!--product still in list, wait consumer to get it..
:::Consumer fetch: 99
!--No product now, wait producer put in...
:::Producer put: 12
:::Consumer fetch: 12
:::Producer put: 91
!--product still in list, wait consumer to get it..
!--product still in list, wait consumer to get it..
:::Consumer fetch: 91
!--No product now, wait producer put in...
:::Producer put: 63
:::Consumer fetch: 63
:::Producer put: 85
!--product still in list, wait consumer to get it..
!--product still in list, wait consumer to get it..
:::Consumer fetch: 85
!--No product now, wait producer put in...
:::Producer put: 1
:::Consumer fetch: 1
:::Producer put: 26
!--product still in list, wait consumer to get it..
!--product still in list, wait consumer to get it..
:::Consumer fetch: 26
!--No product now, wait producer put in...
:::Producer put: 8
:::Consumer fetch: 8
:::Producer put: 19
!--product still in list, wait consumer to get it..
!--product still in list, wait consumer to get it..
:::Consumer fetch: 19
!--No product now, wait producer put in...
:::Producer put: 74
!--product still in list, wait consumer to get it..
:::Consumer fetch: 74
:::Producer put: 50
!--product still in list, wait consumer to get it..
:::Consumer fetch: 50
!--No product now, wait producer put in...
:::Producer put: 97
:::Consumer fetch: 97
:::Producer put: 69
!--product still in list, wait consumer to get it..
!--product still in list, wait consumer to get it..
:::Consumer fetch: 69
!--No product now, wait producer put in...
:::Producer put: 41
!--product still in list, wait consumer to get it..
:::Consumer fetch: 41
:::Producer put: 6
!--product still in list, wait consumer to get it..
:::Consumer fetch: 6
!--No product now, wait producer put in...

以上是“使用Python實現生產者、消費者問題”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

泽州县| 靖宇县| 锦屏县| 西平县| 铜梁县| 盱眙县| 滦平县| 中江县| 平湖市| 南皮县| 得荣县| 沭阳县| 巴东县| 马边| 玉田县| 广饶县| 新丰县| 河池市| 江孜县| 隆子县| 榕江县| 巫山县| 兖州市| 安龙县| 鄂州市| 盖州市| 阿克陶县| 平罗县| 六盘水市| 日照市| 敦化市| 麻栗坡县| 石渠县| 锦州市| 宁南县| 新巴尔虎左旗| 敖汉旗| 射洪县| 隆德县| 抚远县| 安宁市|