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

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

 更新時(shí)間:2018年05月30日 10:01:11   作者:shw800  
這篇文章主要介紹了Python實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者問(wèn)題,簡(jiǎn)單描述了生產(chǎn)者、消費(fèi)者問(wèn)題的概念、原理,并結(jié)合完整實(shí)例形式分析了Python實(shí)現(xiàn)生產(chǎn)者、消費(fèi)者問(wèn)題的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者問(wèn)題。分享給大家供大家參考,具體如下:

生產(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類(lèi),獲取鎖對(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...

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python數(shù)學(xué)運(yùn)算技巧總結(jié)

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • python實(shí)戰(zhàn)串口助手_解決8串口多個(gè)發(fā)送的問(wèn)題

    python實(shí)戰(zhàn)串口助手_解決8串口多個(gè)發(fā)送的問(wèn)題

    今天小編就為大家分享一篇python實(shí)戰(zhàn)串口助手_解決8串口多個(gè)發(fā)送的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • python利用PyQt5設(shè)計(jì)鼠標(biāo)顯示形狀

    python利用PyQt5設(shè)計(jì)鼠標(biāo)顯示形狀

    不知道大家有沒(méi)有發(fā)現(xiàn),我們?cè)诰W(wǎng)頁(yè)移動(dòng)鼠標(biāo)時(shí),不同的網(wǎng)頁(yè)會(huì)有不同的鼠標(biāo)移動(dòng)特效,通過(guò)移動(dòng)鼠標(biāo),會(huì)形成類(lèi)似蜘蛛網(wǎng)等等的特效,本文將用PyQt5實(shí)現(xiàn)這一特效,需要的可以參考一下
    2024-07-07
  • Python+Selenium實(shí)現(xiàn)一鍵摸魚(yú)&采集數(shù)據(jù)

    Python+Selenium實(shí)現(xiàn)一鍵摸魚(yú)&采集數(shù)據(jù)

    將Selenium程序編寫(xiě)為 .bat 可執(zhí)行文件,從此一鍵啟動(dòng)封裝好的Selenium程序,省時(shí)省力還可以復(fù)用,豈不美哉。所以本文將利用Selenium實(shí)現(xiàn)一鍵摸魚(yú)&一鍵采集數(shù)據(jù),需要的可以參考一下
    2022-08-08
  • python3中bytes數(shù)據(jù)類(lèi)型的具體使用

    python3中bytes數(shù)據(jù)類(lèi)型的具體使用

    bytes類(lèi)型是python3引入的,本文就來(lái)介紹一下python3中bytes數(shù)據(jù)類(lèi)型的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • Python分析特征數(shù)據(jù)類(lèi)別與預(yù)處理方法速學(xué)

    Python分析特征數(shù)據(jù)類(lèi)別與預(yù)處理方法速學(xué)

    這篇文章主要為大家介紹了Python分析特征數(shù)據(jù)類(lèi)別與預(yù)處理方法速學(xué),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Pytorch的torch.nn.embedding()如何實(shí)現(xiàn)詞嵌入層

    Pytorch的torch.nn.embedding()如何實(shí)現(xiàn)詞嵌入層

    這篇文章主要介紹了Pytorch的torch.nn.embedding()如何實(shí)現(xiàn)詞嵌入層問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python BeautifulReport可視化報(bào)告代碼實(shí)例

    Python BeautifulReport可視化報(bào)告代碼實(shí)例

    這篇文章主要介紹了Python BeautifulReport可視化報(bào)告代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Python上下文管理器Content Manager

    Python上下文管理器Content Manager

    在Python中,我們會(huì)經(jīng)常聽(tīng)到上下文管理器,那么上下文管理器到底是干什么的,本文就來(lái)介紹一下,感興趣的小伙伴們可以參考一下
    2021-06-06
  • python之NAN和INF值處理方式

    python之NAN和INF值處理方式

    這篇文章主要介紹了python之NAN和INF值處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Python操作Elasticsearch處理timeout超時(shí)

    Python操作Elasticsearch處理timeout超時(shí)

    這篇文章主要介紹了Python操作Elasticsearch處理timeout超時(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07

最新評(píng)論

吉木萨尔县| 石泉县| 五家渠市| 合阳县| 屯门区| 南开区| 巩留县| 合阳县| 且末县| 梨树县| 武鸣县| 碌曲县| 日照市| 万山特区| 无锡市| 中山市| 崇信县| 额济纳旗| 青海省| 东光县| 洪泽县| 浙江省| 冀州市| 西藏| 民和| 蒲江县| 五指山市| 勐海县| 来凤县| 招远市| 西乌珠穆沁旗| 澄江县| 古浪县| 双峰县| 抚松县| 平武县| 东阳市| 吴忠市| 巴马| 冀州市| 宁波市|