Python實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者問題完整實(shí)例
本文實(shí)例講述了Python實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者問題。分享給大家供大家參考,具體如下:
生產(chǎn)者、消費(fèi)者問題,經(jīng)典的線程同步問題:假設(shè)有一個(gè)緩沖池(列表),生產(chǎn)者往里面放東西,消費(fèi)者從里面取,規(guī)則是:列表為空的時(shí)候,生產(chǎn)者才能放東西;列表不為空的時(shí)候,消費(fèi)者才能取東西;為了簡(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...
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python使用matplotlib畫出的圖怎樣放到word中
這篇文章主要介紹了python使用matplotlib畫出的圖怎樣放到word中問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09python利用opencv實(shí)現(xiàn)顏色檢測(cè)
這篇文章主要為大家詳細(xì)介紹了python利用opencv實(shí)現(xiàn)顏色檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02利用python在excel中畫圖的實(shí)現(xiàn)方法
這篇文章主要介紹了利用python在excel中畫圖的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03python從ftp下載數(shù)據(jù)保存實(shí)例
這篇文章主要介紹了python從ftp下載數(shù)據(jù)到本地保存的實(shí)例代碼方法,大家參考使用吧2013-11-11pyqt QGraphicsView 以鼠標(biāo)為中心進(jìn)行縮放功能實(shí)現(xiàn)
在PyQt開發(fā)中,實(shí)現(xiàn)QGraphicsView的鼠標(biāo)中心縮放功能需要注意初始化以及關(guān)鍵函數(shù)的重定義,遇到不達(dá)預(yù)期的效果時(shí),可能需要重寫所有鼠標(biāo)事件,本文記錄了解決QGraphicsView鼠標(biāo)縮放問題的過程,供開發(fā)者參考2024-10-10python 解決print數(shù)組/矩陣無法完整輸出的問題
這篇文章主要介紹了關(guān)于python 解決print數(shù)組/矩陣無法完整輸出的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02利用Python自制網(wǎng)頁并實(shí)現(xiàn)一鍵自動(dòng)生成探索性數(shù)據(jù)分析報(bào)告
這篇文章主要介紹了利用Python自制了網(wǎng)頁并實(shí)現(xiàn)一鍵自動(dòng)生成探索性數(shù)據(jù)分析報(bào)告,文章內(nèi)容具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05Anaconda中利用conda創(chuàng)建、激活、刪除、添加新環(huán)境
在使用Python開發(fā)項(xiàng)目或者編寫腳本的時(shí)候通常需要建立不同版本的Python的虛擬環(huán)境,本文主要介紹了Anaconda中利用conda創(chuàng)建、激活、刪除、添加新環(huán)境,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04