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的Django框架中生成CSV文件的方法
Jul 22 Python
Python使用自带的ConfigParser模块读写ini配置文件
Jun 26 Python
python模块之sys模块和序列化模块(实例讲解)
Sep 13 Python
使用Python & Flask 实现RESTful Web API的实例
Sep 19 Python
python3 破解 geetest(极验)的滑块验证码功能
Feb 24 Python
Tensorflow使用支持向量机拟合线性回归
Sep 07 Python
解决pytorch GPU 计算过程中出现内存耗尽的问题
Aug 19 Python
python3.5 cv2 获取视频特定帧生成jpg图片
Aug 28 Python
使用sklearn对多分类的每个类别进行指标评价操作
Jun 11 Python
Django中ORM的基本使用教程
Dec 22 Python
python基础学习之生成器与文件系统知识总结
May 25 Python
使用Django实现商城验证码模块的方法
Jun 01 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上传文件并存储到mysql数据库的方法
2015/03/16 PHP
在WordPress中获取数据库字段内容和添加主题设置菜单
2016/01/11 PHP
简介PHP的Yii框架中缓存的一些高级用法
2016/03/29 PHP
Laravel中七个非常有用但很少人知道的Carbon方法
2017/09/21 PHP
利用php + Laravel如何实现部署自动化详解
2017/10/11 PHP
详解将数据从Laravel传送到vue的四种方式
2019/10/16 PHP
关于使用runtimeStyle属性问题讨论文章
2007/03/08 Javascript
Javascript和HTML5利用canvas构建Web五子棋游戏实现算法
2013/07/17 Javascript
javascript随机将第一个dom中的图片添加到第二个div中示例
2013/10/08 Javascript
我的Node.js学习之路(四)--单元测试
2014/07/06 Javascript
nodejs 子进程正确的打开方式
2017/07/03 NodeJs
新版vue-cli模板下本地开发环境使用node服务器跨域的方法
2018/04/03 Javascript
vue-router 源码实现前端路由的两种方式
2018/07/02 Javascript
vue中倒计时组件的实例代码
2018/07/06 Javascript
使用JavaScript计算前一天和后一天的思路详解
2019/12/20 Javascript
vue实现几秒后跳转新页面代码
2020/09/09 Javascript
[01:33]一分钟玩转DOTA2第三弹:DOTA2&DotA快捷操作大对比
2014/06/04 DOTA
[01:23]一分钟告诉你 DOTA2为什么叫信仰2
2014/06/20 DOTA
[33:15]2018DOTA2亚洲邀请赛3月30日 小组赛B组 VP VS Mineski
2018/03/31 DOTA
[01:33:07]VGJ.T vs Newbee Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
[54:27]TNC vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
python读写ini配置文件方法实例分析
2015/06/30 Python
Django框架之中间件MiddleWare的实现
2019/12/30 Python
Python图片处理模块PIL操作方法(pillow)
2020/04/07 Python
什么是python的自省
2020/06/21 Python
CSS3 实现弹跳的小球动画
2020/10/26 HTML / CSS
html5 canvas 画图教程案例分析
2012/11/23 HTML / CSS
四好少年事迹材料
2014/01/12 职场文书
村抢险救灾方案
2014/05/09 职场文书
我爱家乡演讲稿
2014/09/12 职场文书
庆祝国庆节演讲稿2014
2014/09/19 职场文书
入党积极分子对十八届四中全会期盼的思想汇报
2014/10/17 职场文书
怎样写离婚协议书
2015/01/26 职场文书
体检通知范文
2015/04/21 职场文书
2016元旦文艺汇演主持词
2015/07/06 职场文书
二十年同学聚会致辞
2015/07/28 职场文书