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计算回文数的方法
Mar 11 Python
Android模拟器无法启动,报错:Cannot set up guest memory ‘android_arm’ Invalid argument的解决方法
Jul 01 Python
python图片验证码生成代码
Jul 02 Python
对Python使用mfcc的两种方式详解
Jan 09 Python
python实现键盘输入的实操方法
Jul 16 Python
python中利用matplotlib读取灰度图的例子
Dec 07 Python
Python二次规划和线性规划使用实例
Dec 09 Python
Python API len函数操作过程解析
Mar 05 Python
详解python实现可视化的MD5、sha256哈希加密小工具
Sep 14 Python
python实现猜拳游戏项目
Nov 30 Python
python推导式的使用方法实例
Feb 28 Python
Python快速优雅的批量修改Word文档样式
May 20 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
基于php无限分类的深入理解
2013/06/02 PHP
php车辆违章查询数据示例
2016/10/14 PHP
PHP中define() 与 const定义常量的区别详解
2019/06/25 PHP
JavaScript 设计模式 富有表现力的Javascript(一)
2010/05/26 Javascript
JS中 用户登录系统的解决办法
2013/04/15 Javascript
javascript检查浏览器是否支持flash的实现代码
2014/08/14 Javascript
jquery实现图片随机排列的方法
2015/05/04 Javascript
JS实现的通用表单验证插件完整实例
2015/08/20 Javascript
js中substr,substring,indexOf,lastIndexOf,split,replace的用法详解
2015/11/09 Javascript
jquery对象访问是什么及使用方法介绍
2016/05/03 Javascript
微信小程序 定义全局数据、函数复用、模版等详细介绍
2016/10/27 Javascript
微信小程序 textarea 组件详解及简单实例
2017/01/10 Javascript
解决nodejs中使用http请求返回值为html时乱码的问题
2017/02/18 NodeJs
基于JavaScript实现验证码功能
2017/04/01 Javascript
JQuery 获取Dom元素的实例讲解
2017/07/08 jQuery
Vue-Access-Control 前端用户权限控制解决方案
2017/12/01 Javascript
用JS实现根据当前时间随机生成流水号或者订单号
2018/05/31 Javascript
layui 实现表单和文件上传一起传到后台的例子
2019/09/16 Javascript
mpvue实现小程序签到金币掉落动画(api实现)
2019/10/17 Javascript
jQuery实现简单三级联动效果
2020/09/05 jQuery
python之import机制详解
2014/07/03 Python
Python字符串拼接、截取及替换方法总结分析
2016/04/13 Python
Python 获取当前所在目录的方法详解
2017/08/02 Python
pip安装Python库时遇到的问题及解决方法
2017/11/23 Python
python接口调用已训练好的caffe模型测试分类方法
2019/08/26 Python
浅谈OpenCV中的新函数connectedComponentsWithStats用法
2020/07/05 Python
Python SMTP发送电子邮件的示例
2020/09/23 Python
Html5 video标签视频的最佳实践
2020/02/26 HTML / CSS
size?法国官网:英国伦敦的球鞋精品店
2020/03/15 全球购物
材料工程专业毕业生求职信
2014/03/04 职场文书
电子商务专业自荐信
2014/06/02 职场文书
安全横幅标语
2014/06/09 职场文书
征兵宣传标语
2014/06/20 职场文书
2014年中秋寄语
2014/08/11 职场文书
销售经理岗位职责
2015/01/31 职场文书
2015医院个人工作总结范文
2015/05/21 职场文书