您好,登錄后才能下訂單哦!
這篇文章主要介紹使用Python實現生產者、消費者問題,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
生產者、消費者問題,經典的線程同步問題:假設有一個緩沖池(列表),生產者往里面放東西,消費者從里面取,規則是:列表為空的時候,生產者才能放東西;列表不為空的時候,消費者才能取東西;為了簡單起見,暫定緩沖池中最多只能有一個產品。這里生產者和消費者共同操作一個資源:緩沖池,因此每次操作的時候,需要給資源加鎖,操作結束時,釋放鎖,這樣才能做到資源同步。使用python實現,需要繼承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()
這里使用多個生產者和消費者,共同操作緩沖池,部分執行結果如下:
:::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實現生產者、消費者問題”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。