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清除字符串里非数字字符的方法
Jul 02 Python
python学习教程之使用py2exe打包
Sep 24 Python
python删除服务器文件代码示例
Feb 09 Python
Python使用Pickle库实现读写序列操作示例
Jun 15 Python
python遍历文件夹找出文件夹后缀为py的文件方法
Oct 21 Python
jupyter notebook 中输出pyecharts图实例
Apr 23 Python
python写入数据到csv或xlsx文件的3种方法
Aug 23 Python
python安装gdal的两种方法
Oct 29 Python
Python3 虚拟开发环境搭建过程(图文详解)
Jan 06 Python
使用python批量修改XML文件中图像的depth值
Jul 22 Python
教你怎么用Python实现多路径迷宫
Apr 29 Python
教你使用Python获取QQ音乐某个歌手的歌单
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
Bo-Blog专用的给Windows服务器的IIS Rewrite程序
2007/08/26 PHP
PHP下对字符串的递增运算代码
2010/08/21 PHP
CI框架开发新浪微博登录接口源码完整版
2014/05/28 PHP
php数组合并与拆分实例分析
2015/06/12 PHP
PHP开发中AJAX技术的简单应用
2015/12/11 PHP
phpinfo()中Loaded Configuration File(none)的解决方法
2017/01/16 PHP
如何重写Laravel异常处理类详解
2020/12/20 PHP
ie与ff下的event事件使用介绍
2013/11/25 Javascript
设置jsf的选择框h:selectOneMenu为不可编辑状态的方法
2014/01/07 Javascript
javascript中加号(+)操作符的一些神奇作用
2014/06/06 Javascript
Javascript中数组sort和reverse用法分析
2014/12/30 Javascript
js简单的点击返回顶部效果实现方法
2015/04/10 Javascript
javascript垃圾收集机制的原理分析
2016/12/08 Javascript
bootstrap提示标签、提示框实现代码
2016/12/28 Javascript
jQuery插件zTree实现更新根节点中第i个节点名称的方法示例
2017/03/08 Javascript
nodejs入门教程一:概念与用法简介
2017/04/24 NodeJs
基于Vue.js 2.0实现百度搜索框效果
2020/12/28 Javascript
QQ跳转支付宝并自动领红包脚本(最新)
2018/06/22 Javascript
p5.js临摹动态图形实现方法详解
2019/10/23 Javascript
webpack proxy 使用(代理的使用)
2020/01/10 Javascript
[56:45]DOTA2上海特级锦标赛D组小组赛#1 EG VS COL第一局
2016/02/28 DOTA
[05:20]2018DOTA2亚洲邀请赛主赛事第三日战况回顾 LGD率先挺进胜者组决赛
2018/04/06 DOTA
[01:10]DOTA2 Supermajor:英雄,由我们见证
2018/05/14 DOTA
零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
2014/11/05 Python
详解Python中映射类型的内建函数和工厂函数
2015/08/19 Python
Python实现一个服务器监听多个客户端请求
2018/04/12 Python
Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法
2020/04/18 Python
Django之腾讯云短信的实现
2020/06/12 Python
Python pymysql模块安装并操作过程解析
2020/10/13 Python
新加坡航空官方网站:Singapore Airlines
2016/10/13 全球购物
乌克兰珠宝大卖场:Zlato.ua
2020/09/27 全球购物
建筑专业自荐信
2013/10/18 职场文书
学雷锋志愿者活动总结
2014/06/27 职场文书
2015年法院工作总结范文
2015/04/28 职场文书
2019个人半年工作总结
2019/06/21 职场文书
Python查找算法的实现 (线性、二分,分块、插值查找算法)
2022/04/24 Python