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脚本实现DNSPod DNS动态解析域名
Feb 14 Python
Python标准库defaultdict模块使用示例
Apr 28 Python
Python实现网络端口转发和重定向的方法
Sep 19 Python
利用Python爬虫给孩子起个好名字
Feb 14 Python
Python字典操作详细介绍及字典内建方法分享
Jan 04 Python
python解析html提取数据,并生成word文档实例解析
Jan 22 Python
详解Python发送email的三种方式
Oct 18 Python
PyCharm中代码字体大小调整方法
Jul 29 Python
浅谈django2.0 ForeignKey参数的变化
Aug 06 Python
python3.6生成器yield用法实例分析
Aug 23 Python
python、PyTorch图像读取与numpy转换实例
Jan 13 Python
Django CBV模型源码运行流程详解
Aug 17 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教程孙仲岳主讲
2008/01/07 PHP
PHP 防注入函数(格式化数据)
2011/08/08 PHP
Linux系统中设置多版本PHP共存配合Nginx服务器使用
2015/12/21 PHP
给大家分享几个常用的PHP函数
2017/01/15 PHP
使用CSS3实现字体颜色渐变的实现
2021/03/09 HTML / CSS
jquery 插件开发方法小结
2009/10/23 Javascript
JavaScript输出当前时间Unix时间戳的方法
2015/04/06 Javascript
10道典型的JavaScript面试题
2017/03/22 Javascript
ES6扩展运算符的用途实例详解
2017/08/20 Javascript
jquery使用FormData实现异步上传文件
2018/10/25 jQuery
浅谈Vue 性能优化之深挖数组
2018/12/11 Javascript
Vue+Express实现登录状态权限验证的示例代码
2019/05/05 Javascript
Vue实现回到顶部和底部动画效果
2019/07/31 Javascript
no-vnc和node.js实现web远程桌面的完整步骤
2019/08/11 Javascript
5个你不知道的JavaScript字符串处理库(小结)
2020/06/01 Javascript
详解Vue.js 响应接口
2020/07/04 Javascript
vue+swiper实现左右滑动的测试题功能
2020/10/30 Javascript
在Python中移动目录结构的方法
2016/01/31 Python
Python使用functools实现注解同步方法
2018/02/06 Python
python实现简单遗传算法
2018/03/19 Python
详解python调用cmd命令三种方法
2019/07/08 Python
使用python对多个txt文件中的数据进行筛选的方法
2019/07/10 Python
python的列表List求均值和中位数实例
2020/03/03 Python
Python计算指定日期是今年的第几天(三种方法)
2020/03/26 Python
Python求凸包及多边形面积教程
2020/04/12 Python
python实现画图工具
2020/08/27 Python
手把手教你配置JupyterLab 环境的实现
2021/02/02 Python
违反课堂纪律检讨书
2014/01/19 职场文书
文艺晚会策划方案
2014/06/11 职场文书
班级团队活动方案
2014/08/14 职场文书
药店促销活动策划方案
2014/08/24 职场文书
2014年物业公司工作总结
2014/11/22 职场文书
学雷锋团日活动总结
2015/05/06 职场文书
教师廉政准则心得体会
2016/01/20 职场文书
世界文化遗产导游词
2019/08/07 职场文书
Java9新特性对HTTP2协议支持与非阻塞HTTP API
2022/03/16 Java/Android