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

作者:shw800 时间:2022-08-15 14:37:30 

本文实例讲述了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程序设计有所帮助。

来源:https://blog.csdn.net/shw800/article/details/46468473

标签:Python,生产者,消费者,问题
0
投稿

猜你喜欢

  • Mysql删除重复数据并且只保留一条(附实例!)

    2024-01-27 04:48:27
  • CSS属性behavior的语法及介绍

    2010-01-13 12:40:00
  • 实用的 vue tags 创建缓存导航的过程实现

    2024-05-29 22:45:55
  • Pandas分组与排序的实现

    2022-11-27 14:13:18
  • 爱你就要说出来,来表白吧

    2008-11-23 16:23:00
  • Python的爬虫程序编写框架Scrapy入门学习教程

    2021-12-27 10:01:48
  • pygame游戏之旅 游戏中添加显示文字

    2023-03-26 02:56:23
  • js下用eval生成JSON对象

    2024-04-19 10:00:24
  • python代码打印100-999之间的回文数示例

    2022-10-14 02:53:39
  • PHPExcel笔记, mpdf导出

    2024-06-05 09:44:57
  • Python获取文件所在目录和文件名的方法

    2021-04-07 05:44:35
  • django创建简单的页面响应实例教程

    2021-05-15 17:05:08
  • Pygame游戏开发之太空射击实战碰撞改进篇

    2022-11-19 03:04:52
  • Python用dilb提取照片上人脸的示例

    2021-07-04 23:34:47
  • Python提取频域特征知识点浅析

    2021-10-31 08:01:31
  • 详解Python判定IP地址合法性的三种方法

    2021-12-02 14:35:10
  • Mini,又个 Javascript 选择器

    2009-10-06 14:48:00
  • MySql批量插入时如何不重复插入数据

    2024-01-29 04:42:00
  • python实现层次聚类的方法

    2023-05-03 22:26:13
  • Dreamweaver 4.0使用技巧集锦

    2007-11-08 12:47:00
  • asp之家 网络编程 m.aspxhome.com