网络编程
位置:首页>> 网络编程>> Python编程>> Python之两种模式的生产者消费者模型详解

Python之两种模式的生产者消费者模型详解

作者:haeasringnar  发布时间:2021-07-31 17:44:02 

标签:Python,生产者,消费者

第一种使用queue队列实现:


#生产者消费者模型 其实服务器集群就是这个模型
# 这里介绍的是非yield方法实现过程

import threading,time
import queue
q = queue.Queue(maxsize=10)

def Producer(anme):
# for i in range(10):
#  q.put('骨头%s'%i)
count = 1
while True:
 q.put('骨头%s'%count)
 print('生产了骨头',count)
 count += 1
 time.sleep(1)

def Consumer(name):
# while q.qsize() >0:
while True:
 print('[%s] 取到[%s] 并且吃了它...'%(name,q.get()))
 time.sleep(1)

p = threading.Thread(target=Producer,args=('shenchanzhe',))
c = threading.Thread(target=Consumer,args=('xiaofeizhe01',))
c1 = threading.Thread(target=Consumer,args=('xiaofeizhe02',))

p.start()
c.start()
c1.start()

使用yield协程的方法来实现生产者和消费者:


#生产者和消费者,使用生成器的方式,就是一个简单的并行,
import time
# 这是一个消费者 一直在等待完成吃包子的动作
def consumer(name):
print('%s准备吃包子了!'%name) #打印出对应的消费者的名字
while True: #执行一个死循环 实际上就是需要调用时才会执行,没有调用就会停止在yield
 baozi = yield #在它就收到内容的时候后就把内容传给baozi
 print('包子【%s】来了,被【%s】吃了'%(baozi,name))
def producer(name):
c1 = consumer('A') #它只是把c1变成一个生成器
c2 = consumer('B')
c1.__next__() #第一个next只是会走到yield然后停止
c2.__next__()
print('老子开始做包子了')
for i in range(1,10):
 time.sleep(1)
 print('三秒做了两个包子')
 c1.send(i) #这一步其实就是调用next方法的同时传一个参数i给field接收,然后baozi=i
 c2.send(i+1)
 #其实这里是这样的,在send的时候只是继续执行yield下面的语句,然后去去yield,再次停在这儿

# producer('aea')
c = consumer('aaa') #没next一次就会将程序执行一次
c.__next__()
c.__next__()
c.__next__()

来源:https://blog.csdn.net/haeasringnar/article/details/79917253

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com