欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-08-14 19:52:32
本文实例讲述了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程序设计有所帮助。