Python實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者問(wèn)題完整實(shí)例
本文實(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)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
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ù)
將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)型的具體使用
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é),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
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í)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Python操作Elasticsearch處理timeout超時(shí)
這篇文章主要介紹了Python操作Elasticsearch處理timeout超時(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

