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开发中module模块用法实例分析
Nov 12 Python
Django 添加静态文件的两种实现方法(必看篇)
Jul 14 Python
Python 模拟购物车的实例讲解
Sep 11 Python
《与孩子一起学编程》python自测题
May 27 Python
Python同步遍历多个列表的示例
Feb 19 Python
Python任意字符串转16, 32, 64进制的方法
Jun 12 Python
使用python打印十行杨辉三角过程详解
Jul 10 Python
解决使用export_graphviz可视化树报错的问题
Aug 09 Python
Python中zip()函数的简单用法举例
Sep 02 Python
Python json模块与jsonpath模块区别详解
Mar 05 Python
Python 统计位数为偶数的数字代码详解
Mar 15 Python
django注册用邮箱发送验证码的实现
Apr 18 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 将逗号、空格、回车分隔的字符串转换为数组的函数
2012/06/07 PHP
PHP 中关于ord($str)>0x80的详细说明
2012/09/23 PHP
php简单压缩css样式示例
2016/09/22 PHP
如何实现JS函数的重载
2006/09/22 Javascript
JQuery下关于$.Ready()的分析
2009/12/13 Javascript
js写出遮罩层登陆框和对联广告并自动跟随滚动条滚动
2014/04/29 Javascript
jQuery对象初始化的传参方式
2015/02/26 Javascript
获取今天,昨天,本周,上周,本月,上月时间(实例分享)
2017/01/04 Javascript
JavaScript日期选择功能示例
2017/01/16 Javascript
MUI 上拉刷新/下拉加载功能实例代码
2017/04/13 Javascript
AngularJS实用基础知识_入门必备篇(推荐)
2017/07/10 Javascript
详解vue-cli之webpack3构建全面提速优化
2017/12/25 Javascript
老生常谈JS中的继承及实现代码
2018/07/06 Javascript
浅谈angular2子组件的事件传递(任意组件事件传递)
2018/09/30 Javascript
JS使用Prim算法和Kruskal算法实现最小生成树
2019/01/17 Javascript
JS中数组实现代码(倒序遍历数组,数组连接字符串)
2019/12/29 Javascript
[01:04:35]2018DOTA2亚洲邀请赛 4.3 突围赛 Secret vs VG 第一场
2018/04/04 DOTA
Python检测QQ在线状态的方法
2015/05/09 Python
Python安装模块的常见问题及解决方法
2018/02/05 Python
python opencv之SIFT算法示例
2018/02/24 Python
python爬虫_实现校园网自动重连脚本的教程
2018/04/22 Python
Python实现的爬虫刷回复功能示例
2018/06/07 Python
python遍历文件夹,指定遍历深度与忽略目录的方法
2018/07/11 Python
Python多进程写入同一文件的方法
2019/01/14 Python
用pycharm开发django项目示例代码
2019/06/13 Python
python如何将多个PDF进行合并
2019/08/13 Python
python 实现数据库中数据添加、查询与更新的示例代码
2020/12/07 Python
英国电子专家:maplin
2019/09/04 全球购物
P D PAOLA意大利官网:西班牙著名的珠宝首饰品牌
2019/09/24 全球购物
《狐假虎威》教学反思
2014/02/07 职场文书
优秀广告词大全
2014/03/19 职场文书
淘宝客服专员岗位职责
2014/04/11 职场文书
社区党员志愿服务活动方案
2014/08/18 职场文书
党员民主生活会个人整改措施材料
2014/09/16 职场文书
家庭财产分割协议范文
2014/11/24 职场文书
管辖权异议上诉状
2015/05/23 职场文书