之前用c++寫過一篇生產者消費者的實現。
生產者和消費者主要是處理互斥和同步的問題:
佇列作為緩衝區,需要互斥操作
佇列中沒有產品,消費者需要等待,直到生產者放入產品並通知它。佇列慢的情況類似。這裡我使用list模擬python標準庫的queue,這裡我設定乙個大小限制為5:
syncqueue.py
from threading importlock
from threading import
condition
class
queue():
def__init__
(self):
self.mutex =lock()
self.full =condition(self.mutex)
self.empty =condition(self.mutex)
self.data =
defpush(self, element):
self.mutex.acquire()
while len(self.data) >= 5:
self.empty.wait()
self.full.notify()
self.mutex.release()
defpop(self):
self.mutex.acquire()
while len(self.data) ==0:
self.full.wait()
data =self.data[0]
self.data.pop(0)
self.empty.notify()
self.mutex.release()
return
data
if__name__ == '
__main__':
q =queue()
q.push(10)
q.push(2)
q.push(13)
q.pop()
q.pop()
print q.pop()
這是最核心的**,注意裡面判斷條件要使用while迴圈。
接下來是生產者程序,producer.py
from threading importthread
from random import
randrange
from time import
sleep
from syncqueue import
queue
class
producerthread(thread):
def__init__
(self, queue):
thread.
__init__
(self)
self.queue =queue
defrun(self):
while
true:
data = randrange(0, 100)
self.queue.push(data)
'push %d
' %(data)
sleep(1)
if__name__ == '
__main__':
q =queue()
t =producerthread(q)
t.start()
t.join()
消費者,condumer.py
from threading importthread
from time import
sleep
from syncqueue import
queue
class
consumerthread(thread):
def__init__
(self, queue):
thread.
__init__
(self)
self.queue =queue
defrun(self):
while
true:
data =self.queue.pop()
'pop %d
' %(data)
sleep(1)
if__name__ == '
__main__':
q =queue()
t =consumerthread(q)
t.start()
t.join()
最後我們寫乙個車間類,可以指定執行緒數量:
from syncqueue importqueue
from producer import
producerthread
from consumer import
consumerthread
class
workshop():
def__init__
(self, producernums, consumernums):
self.producers =
self.consumers =
self.queue =queue()
self.producernums =producernums
self.consumernums =consumernums
defstart(self):
for i in
range(self.producernums):
for i in
range(self.consumernums):
for i in
range(len(self.producers)):
self.producers[i].start()
for i in
range(len(self.consumers)):
self.consumers[i].start()
for i in
range(len(self.producers)):
self.producers[i].join()
for i in
range(len(self.consumers)):
self.consumers[i].join()
if__name__ == '
__main__':
w = workshop(3, 4)
w.start()
最後寫乙個main模組:
from workshop importworkshop
if__name__ == '
__main__':
w = workshop(2, 3)
w.start()
python 實現生產者消費者模型
生產者消費者 包子鋪不停的做包子,行人不停的買 這樣就達到了目的 包子的銷售 兩個不同的角色 包子鋪,行人 只負責單一操作 讓包子變成連線的介質.1 coding utf 8 2from threading import thread 3from queue import queue 4import...
python 生產者 消費者
from bs4 import beautifulsoup import requests import time import multiprocessing as mp import re from multiprocessing import queue from multiprocessin...
生產者消費者 生產者與消費者模式
一 什麼是生產者與消費者模式 其實生產者與消費者模式就是乙個多執行緒併發協作的模式,在這個模式中呢,一部分執行緒被用於去生產資料,另一部分執行緒去處理資料,於是便有了形象的生產者與消費者了。而為了更好的優化生產者與消費者的關係,便設立乙個緩衝區,也就相當於乙個資料倉儲,當生產者生產資料時鎖住倉庫,不...