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实现html转ubb代码(html2ubb)
Jul 03 Python
django创建自定义模板处理器的实例详解
Aug 14 Python
python使用fork实现守护进程的方法
Nov 16 Python
用Python shell简化开发
Aug 08 Python
python画一个玫瑰和一个爱心
Aug 18 Python
python脚本开机自启的实现方法
Jun 28 Python
python读写csv文件的方法
Aug 13 Python
原来我一直安装 Python 库的姿势都不对呀
Nov 11 Python
python数据分析工具之 matplotlib详解
Apr 09 Python
python多进程使用函数封装实例
May 02 Python
keras自动编码器实现系列之卷积自动编码器操作
Jul 03 Python
python爬虫今日热榜数据到txt文件的源码
Feb 23 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
兼容ie6浏览器的php下载文件代码分享
2014/07/14 PHP
thinkphp autoload 命名空间自定义 namespace
2015/07/17 PHP
php 无限分类 树形数据格式化代码
2016/10/11 PHP
php可变长参数处理函数详解
2017/02/22 PHP
laravel5.2表单验证,并显示错误信息的实例
2019/09/29 PHP
JavaScript.Encode手动解码技巧
2010/07/14 Javascript
setTimeout的延时为0时多个浏览器的区别
2012/05/23 Javascript
js判断undefined变量类型使用typeof
2013/06/03 Javascript
js中浮点型运算BUG的解决方法说明
2014/01/06 Javascript
购物车选中得到价格实现示例
2014/01/26 Javascript
JS实现的表格行上下移动操作示例
2016/08/03 Javascript
vue-cli结合Element-ui基于cropper.js封装vue实现图片裁剪组件功能
2018/03/01 Javascript
angular6.0使用教程之父组件通过url传递id给子组件的方法
2018/06/30 Javascript
JavaScript设计模式之建造者模式实例教程
2018/07/02 Javascript
Vue 实现手动刷新组件的方法
2019/02/19 Javascript
详解vue使用插槽分发内容slot的用法
2019/03/28 Javascript
django中使用vue.js的要点总结
2019/07/07 Javascript
Node.JS获取GET,POST数据之queryString模块使用方法详解
2020/02/06 Javascript
vue router-link 默认a标签去除下划线的实现
2020/11/06 Javascript
如何基于viewport vm适配移动端页面
2020/11/13 Javascript
[01:46]新英雄登场
2019/09/10 DOTA
pyqt4教程之实现windows窗口小示例分享
2014/03/07 Python
Python中__new__与__init__方法的区别详解
2015/05/04 Python
vscode 远程调试python的方法
2017/12/01 Python
Python用for循环实现九九乘法表
2018/05/31 Python
python 读取竖线分隔符的文本方法
2018/12/20 Python
爱淘宝:淘宝网购物分享平台
2017/04/28 全球购物
自荐信要包含哪些内容
2013/11/06 职场文书
超市营业员岗位职责
2013/12/20 职场文书
如何写一份好的自荐信
2014/01/02 职场文书
《陈毅探母》教学反思
2014/05/01 职场文书
领导干部群众路线教育实践活动剖析材料
2014/10/10 职场文书
医护人员继续教育学习心得体会
2016/01/19 职场文书
《穷人》教学反思
2016/02/19 职场文书
公文写作:工伤事故分析报告怎么写?
2019/11/05 职场文书
python批量更改目录名/文件名的方法
2021/04/18 Python