Python实现的生产者、消费者问题完整实例


Posted in Python onMay 30, 2018

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
Python实现简单截取中文字符串的方法
Jun 15 Python
Python使用QRCode模块生成二维码实例详解
Jun 14 Python
Django自定义分页效果
Jun 27 Python
[原创]python爬虫(入门教程、视频教程)
Jan 08 Python
如何利用python制作时间戳转换工具详解
Sep 12 Python
解析Python的缩进规则的使用
Jan 16 Python
python 去除二维数组/二维列表中的重复行方法
Jan 23 Python
Django多数据库的实现过程详解
Aug 01 Python
10个Python面试常问的问题(小结)
Nov 20 Python
python能自学吗
Jun 18 Python
pip 20.3 新版本发布!即将抛弃 Python 2.x(推荐)
Dec 16 Python
PyTorch device与cuda.device用法
Apr 03 Python
Django 忘记管理员或忘记管理员密码 重设登录密码的方法
May 30 #Python
解决Django数据库makemigrations有变化但是migrate时未变动问题
May 30 #Python
Python实现的本地文件搜索功能示例【测试可用】
May 30 #Python
Pycharm 创建 Django admin 用户名和密码的实例
May 30 #Python
Django使用详解:ORM 的反向查找(related_name)
May 30 #Python
Python实现决策树C4.5算法的示例
May 30 #Python
python实现决策树ID3算法的示例代码
May 30 #Python
You might like
六酷社区论坛HOME页清新格调免费版 下载
2007/03/07 PHP
使用php统计字符串中中英文字符的个数
2013/06/23 PHP
php实现下载限制速度示例分享
2014/02/13 PHP
php实现获取局域网所有用户的电脑IP和主机名、及mac地址完整实例
2014/07/18 PHP
PHP XML Expat解析器知识点总结
2019/02/15 PHP
PHP过滤器 filter_has_var() 函数用法实例分析
2020/04/23 PHP
TFDN图片播放器 不错自动播放
2006/10/03 Javascript
JavaScript国旗变换效果代码
2008/08/13 Javascript
jquery 插件实现图片延迟加载效果代码
2010/02/06 Javascript
JS读取cookies信息(记录用户名)
2012/01/10 Javascript
使用jQuery内容过滤选择器选择元素实例讲解
2013/04/18 Javascript
点击弹出层效果&弹出窗口后网页背景变暗效果的实现代码
2014/02/10 Javascript
JavaScript编写点击查看大图的页面半透明遮罩层效果实例
2016/05/09 Javascript
JS简单判断滚动条的滚动方向实现方法
2017/04/28 Javascript
JS中的算法与数据结构之链表(Linked-list)实例详解
2019/08/20 Javascript
微信小程序制作扭蛋机代码实例
2019/09/24 Javascript
JavaScript正则表达式验证登录实例
2020/03/18 Javascript
[57:53]DOTA2上海特级锦标赛主赛事日 - 2 败者组第二轮#3OG VS VP
2016/03/03 DOTA
Python 除法小技巧
2008/09/06 Python
Python Web框架Flask中使用七牛云存储实例
2015/02/08 Python
Python中Random和Math模块学习笔记
2015/05/18 Python
python opencv 图像尺寸变换方法
2018/04/02 Python
python清除函数占用的内存方法
2018/06/25 Python
pandas进行数据的交集与并集方式的数据合并方法
2018/06/27 Python
python 模拟贷款卡号生成规则过程解析
2019/08/30 Python
Cython编译python为so 代码加密示例
2019/12/23 Python
python 异步async库的使用说明
2020/05/04 Python
Python 调用C++封装的进一步探索交流
2021/03/04 Python
浅谈CSS3 动画卡顿解决方案
2019/01/02 HTML / CSS
广州御银科技股份有限公司试卷(C++)
2016/11/04 面试题
几个常见的软件测试问题
2016/09/07 面试题
高校教师思想汇报
2014/01/11 职场文书
摄影助理岗位职责
2014/02/07 职场文书
家属联谊会致辞
2015/07/31 职场文书
Mysql关于数据库是否应该使用外键约束详解说明
2021/10/24 MySQL
Rust中的Struct使用示例详解
2022/08/14 Javascript