python之生产者消费者模型实现详解

作者:bainianminguo 时间:2021-12-03 14:37:15 

代码及注释如下


#Auther Bob
#--*--conding:utf-8 --*--
#生产者消费者模型,这里的例子是这样的,有一个厨师在做包子,有一个顾客在吃包子,有一个服务员在储存包子,这个服务员我们就可以用queue来实现
import threading
import queue
import time

'''
def consumer(p,que):
 id = que.get()
 print("[%s]来吃包子了,我吃到的包子的名字是[%s]" %(p,id))

def prodcer(p,que):
 print("[%s]做了2个包子" %(p))
 que.put("baozi[1]")
 print("baozi[1]做好了")
 que.put("baozi[2]")
 print("baozi[2]做好了")

if __name__ == '__main__':
 que = queue.Queue()
 p = threading.Thread(target=prodcer,args=("Bob",que))
 c1 = threading.Thread(target=consumer,args=("c1",que))
 c2 = threading.Thread(target=consumer, args=("c2", que))
 c3 = threading.Thread(target=consumer, args=("c3", que))
 p.start()
 c1.start()
 c2.start()
 c3.start()
 # p.join()

'''

#上面这个例子,如果没有包子了,但是厨师会不知道,厨师也不会继续做包子,而没有吃到包子的人会一直等待,程序会一直不结束

#我们可以这样做,消费者发现没有包子了,告诉服务员,服务员在告诉厨师,这里我们就会遇到task.down

def consumer(p):
 id = que.get()
 print("[%s]来吃包子了,我吃到的包子的名字是[%s]" %(p,id))
 que.task_done()  #如归队列为空了,则会通知que.join,que.join就不会阻塞了

"""

def prodcer(p):
 while True:
   if que.qsize() < 3:
     # time.sleep(1)
     for i in range(2):
       print("[%s]做了包子[%d]" %(p,i))
       que.put(i)
     que.join() #如果队列一直不为空,则que.join会一直阻塞,如果队列为空,则que.join就不阻塞了
"""
def prodcer(p):
 while True:
   # time.sleep(1)
   for i in range(2):
     print("[%s]做了包子[%d]" %(p,i))
     que.put(i)
   que.join() #如果队列一直不为空,则que.join会一直阻塞,如果队列为空,则que.join就不阻塞了
if __name__ == '__main__':
 que = queue.Queue()
 p = threading.Thread(target=prodcer,args=("Bob1",))
 p2 = threading.Thread(target=prodcer, args=("Bob2",))
 c1 = threading.Thread(target=consumer,args=("c1",))
 c2 = threading.Thread(target=consumer, args=("c2",))
 c3 = threading.Thread(target=consumer, args=("c3",))
 c4 = threading.Thread(target=consumer, args=("c4",))
 c5 = threading.Thread(target=consumer, args=("c5",))
 c6 = threading.Thread(target=consumer, args=("c6",))
 p.start()
 p2.start()
 c1.start()
 c2.start()
 c3.start()
 c4.start()
 c5.start()
 c6.start()
 # p.join()
 # que.task_done()

来源:https://www.cnblogs.com/bainianminguo/p/7426273.html

标签:python,生产者,消费者,模型
0
投稿

猜你喜欢

  • Python检测字符串中是否包含某字符集合中的字符

    2023-09-08 10:44:30
  • Python使用Selenium模拟浏览器自动操作功能

    2021-01-19 07:55:33
  • matplotlib grid()设置网格线外观的实现

    2021-08-26 08:39:13
  • jupyter notebook 调用环境中的Keras或者pytorch教程

    2022-10-07 23:23:09
  • CGO编程基础快速入门

    2024-02-05 05:20:58
  • Web设计色彩速查表

    2009-12-21 16:24:00
  • WxPython建立批量录入框窗口

    2023-11-18 15:47:00
  • Python使用pymysql模块操作mysql增删改查实例分析

    2024-01-24 03:56:48
  • Oracle密码错误次数过多账号锁定的问题(推荐)

    2024-01-14 13:24:05
  • python3 模拟登录v2ex实例讲解

    2022-09-23 00:42:53
  • PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)

    2023-10-14 02:31:42
  • JavaScript文档生成工具

    2007-10-26 11:59:00
  • python报错TypeError: Input z must be 2D, not 3D的解决方法

    2023-09-14 09:02:23
  • 使用Django实现把两个模型类的数据聚合在一起

    2023-11-11 23:37:42
  • Python中三种条件语句示例介绍

    2022-09-17 17:28:30
  • asp查询ip地址源代码

    2009-07-27 17:51:00
  • pycharm将英文设置为中文的详细教程

    2022-07-16 14:22:24
  • python flask框架快速入门

    2021-10-16 22:02:26
  • PHP实现的线索二叉树及二叉树遍历方法详解

    2023-11-13 11:28:06
  • python实现协同过滤推荐算法完整代码示例

    2023-10-11 00:29:05
  • asp之家 网络编程 m.aspxhome.com