中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

使用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

拜泉县| 轮台县| 甘肃省| 罗山县| 湖南省| 西充县| 无为县| 湘西| 疏勒县| 鄯善县| 松溪县| 万源市| 盈江县| 金堂县| 鱼台县| 万年县| 武强县| 宣汉县| 南华县| 大同县| 滨州市| 洞口县| 仲巴县| 新安县| 英山县| 留坝县| 朝阳县| 穆棱市| 景德镇市| 焦作市| 麻栗坡县| 武定县| 克什克腾旗| 长兴县| 双牌县| 岗巴县| 遂宁市| 石狮市| 潍坊市| 甘孜| 米脂县|