溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用Python實(shí)現(xiàn)生產(chǎn)者、消費(fèi)者問(wèn)題

發(fā)布時(shí)間:2021-04-09 13:00:16 來(lái)源:億速云 閱讀:437 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹使用Python實(shí)現(xiàn)生產(chǎn)者、消費(fèi)者問(wèn)題,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

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

# -*- 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()

這里使用多個(gè)生產(chǎn)者和消費(fèi)者,共同操作緩沖池,部分執(zhí)行結(jié)果如下:

:::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實(shí)現(xiàn)生產(chǎn)者、消費(fèi)者問(wèn)題”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI