Python實現(xiàn)的生產(chǎn)者、消費者問題完整實例
本文實例講述了Python實現(xiàn)的生產(chǎn)者、消費者問題。分享給大家供大家參考,具體如下:
生產(chǎn)者、消費者問題,經(jīng)典的線程同步問題:假設有一個緩沖池(列表),生產(chǎn)者往里面放東西,消費者從里面取,規(guī)則是:列表為空的時候,生產(chǎn)者才能放東西;列表不為空的時候,消費者才能取東西;為了簡單起見,暫定緩沖池中最多只能有一個產(chǎn)品。這里生產(chǎn)者和消費者共同操作一個資源:緩沖池,因此每次操作的時候,需要給資源加鎖,操作結束時,釋放鎖,這樣才能做到資源同步。使用python實現(xiàn),需要繼承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()
這里使用多個生產(chǎn)者和消費者,共同操作緩沖池,部分執(zhí)行結果如下:
:::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相關內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python數(shù)學運算技巧總結》
希望本文所述對大家Python程序設計有所幫助。
相關文章
python實戰(zhàn)串口助手_解決8串口多個發(fā)送的問題
今天小編就為大家分享一篇python實戰(zhàn)串口助手_解決8串口多個發(fā)送的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python+Selenium實現(xiàn)一鍵摸魚&采集數(shù)據(jù)
將Selenium程序編寫為 .bat 可執(zhí)行文件,從此一鍵啟動封裝好的Selenium程序,省時省力還可以復用,豈不美哉。所以本文將利用Selenium實現(xiàn)一鍵摸魚&一鍵采集數(shù)據(jù),需要的可以參考一下2022-08-08
python3中bytes數(shù)據(jù)類型的具體使用
bytes類型是python3引入的,本文就來介紹一下python3中bytes數(shù)據(jù)類型的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-12-12
Python分析特征數(shù)據(jù)類別與預處理方法速學
這篇文章主要為大家介紹了Python分析特征數(shù)據(jù)類別與預處理方法速學,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Pytorch的torch.nn.embedding()如何實現(xiàn)詞嵌入層
這篇文章主要介紹了Pytorch的torch.nn.embedding()如何實現(xiàn)詞嵌入層問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Python BeautifulReport可視化報告代碼實例
這篇文章主要介紹了Python BeautifulReport可視化報告代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
Python操作Elasticsearch處理timeout超時
這篇文章主要介紹了Python操作Elasticsearch處理timeout超時,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07

