Python实现的生产者、消费者问题完整实例

 更新时间:2018年05月30日 10:01:11   作者:shw800  
这篇文章主要介绍了Python实现的生产者、消费者问题,简单描述了生产者、消费者问题的概念、原理,并结合完整实例形式分析了Python实现生产者、消费者问题的相关操作技巧,需要的朋友可以参考下

本文实例讲述了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函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python数学运算技巧总结

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • python实战串口助手_解决8串口多个发送的问题

    python实战串口助手_解决8串口多个发送的问题

    今天小编就为大家分享一篇python实战串口助手_解决8串口多个发送的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • python利用PyQt5设计鼠标显示形状

    python利用PyQt5设计鼠标显示形状

    不知道大家有没有发现,我们在网页移动鼠标时,不同的网页会有不同的鼠标移动特效,通过移动鼠标,会形成类似蜘蛛网等等的特效,本文将用PyQt5实现这一特效,需要的可以参考一下
    2024-07-07
  • Python+Selenium实现一键摸鱼&采集数据

    Python+Selenium实现一键摸鱼&采集数据

    将Selenium程序编写为 .bat 可执行文件,从此一键启动封装好的Selenium程序,省时省力还可以复用,岂不美哉。所以本文将利用Selenium实现一键摸鱼&一键采集数据,需要的可以参考一下
    2022-08-08
  • python3中bytes数据类型的具体使用

    python3中bytes数据类型的具体使用

    bytes类型是python3引入的,本文就来介绍一下python3中bytes数据类型的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-12-12
  • Python分析特征数据类别与预处理方法速学

    Python分析特征数据类别与预处理方法速学

    这篇文章主要为大家介绍了Python分析特征数据类别与预处理方法速学,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Pytorch的torch.nn.embedding()如何实现词嵌入层

    Pytorch的torch.nn.embedding()如何实现词嵌入层

    这篇文章主要介绍了Pytorch的torch.nn.embedding()如何实现词嵌入层问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Python BeautifulReport可视化报告代码实例

    Python BeautifulReport可视化报告代码实例

    这篇文章主要介绍了Python BeautifulReport可视化报告代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Python上下文管理器Content Manager

    Python上下文管理器Content Manager

    在Python中,我们会经常听到上下文管理器,那么上下文管理器到底是干什么的,本文就来介绍一下,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • python之NAN和INF值处理方式

    python之NAN和INF值处理方式

    这篇文章主要介绍了python之NAN和INF值处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Python操作Elasticsearch处理timeout超时

    Python操作Elasticsearch处理timeout超时

    这篇文章主要介绍了Python操作Elasticsearch处理timeout超时,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07

最新评论