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线程指南详细介绍
Jan 05 Python
Python matplotlib画图实例之绘制拥有彩条的图表
Dec 28 Python
Python实现的圆形绘制(画圆)示例
Jan 31 Python
Python数据分析:手把手教你用Pandas生成可视化图表的教程
Dec 15 Python
Python设计模式之职责链模式原理与用法实例分析
Jan 11 Python
python re库的正则表达式入门学习教程
Mar 08 Python
详解在python操作数据库中游标的使用方法
Nov 12 Python
彻底搞懂 python 中文乱码问题(深入分析)
Feb 28 Python
Python学习之os模块及用法
Jun 03 Python
tensorflow 2.1.0 安装与实战教程(CASIA FACE v5)
Jun 30 Python
零基础学python应该从哪里入手
Aug 11 Python
Python eval函数原理及用法解析
Nov 14 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/12/25 PHP
PHP goto语句简介和使用实例
2014/03/11 PHP
PHP批量检测并去除文件BOM头代码实例
2014/05/08 PHP
Thinkphp 在api开发中异常返回依然是html的解决方式
2019/10/16 PHP
解决laravel查询构造器中的别名问题
2019/10/17 PHP
JavaScript CSS 修改学习第四章 透明度设置
2010/02/19 Javascript
javascript 触发HTML元素绑定的函数
2010/09/11 Javascript
jQuery新闻滚动插件 jquery.roller.js
2011/06/27 Javascript
web的各种前端打印方法之jquery打印插件jqprint实现网页打印
2013/01/09 Javascript
JS脚本defer的作用示例介绍
2014/01/02 Javascript
JavaScript各类型的关系图解
2015/10/16 Javascript
JavaScript判断页面加载完之后再执行预定函数的技巧
2016/05/17 Javascript
利用iscroll4实现轮播图效果实例代码
2017/01/11 Javascript
Bootstrap Tree View简单而优雅的树结构组件实例解析
2017/06/15 Javascript
详述 Sublime Text 打开 GBK 格式中文乱码的解决方法
2017/10/26 Javascript
vue-cli点击实现全屏功能
2020/03/07 Javascript
vue仿携程轮播图效果(滑动轮播,下方高度自适应)
2021/02/11 Vue.js
从零学Python之入门(三)序列
2014/05/25 Python
尝试用最短的Python代码来实现服务器和代理服务器
2016/06/23 Python
Python实现购物系统(示例讲解)
2017/09/13 Python
Python+pandas计算数据相关系数的实例
2018/07/03 Python
在PyCharm下打包*.py程序成.exe的方法
2018/11/29 Python
python实现flappy bird小游戏
2018/12/24 Python
PyTorch中Tensor的维度变换实现
2019/08/18 Python
tensorflow 获取所有variable或tensor的name示例
2020/01/04 Python
TensorFlow2.X使用图片制作简单的数据集训练模型
2020/04/08 Python
实例讲解Python 迭代器与生成器
2020/07/08 Python
Html5新增标签与样式及让元素水平垂直居中
2019/07/11 HTML / CSS
旅游管理专业个人求职信范文
2013/12/24 职场文书
大学生实习感言
2014/01/16 职场文书
德能勤绩廉个人总结
2015/02/14 职场文书
大学生个人学习总结
2015/02/15 职场文书
学生会主席任命书
2015/09/21 职场文书
HTML5简单实现添加背景音乐的几种方法
2021/05/12 HTML / CSS
MySQL修改默认引擎和字符集详情
2021/09/25 MySQL
CSS SandBox应用场景及常见问题
2022/06/25 HTML / CSS