python使用queue队列实现生产者消费者

本文介绍了如何利用Python的queue模块实现生产者消费者问题。通过设置队列容量限制,当队列未满时生产者可以生产,否则等待消费者消费;而消费者在队列不为空时才能消费,否则等待生产。同时提到了threading.Condition也是解决此类问题的一种方式。

queue使用方法,<<详情参考>>
使用queue队列实现生产者消费者问题
另threading.condition也可实现生产者消费者问题,<<详情参考>>

示例:
生产者可以有多个,消费者可以有多个,但是市场容量是有一定限度的
一件产品。投放到市场上,市场需求可以看作一个箱子box,是容量限度
当小于box大小,即队列没有满full,生产者可以生产,否则等待消费者消费,生产者有生产效率,使用speed表示
当产品大于0,即队列不为空null,才可进行消费,否则就等待生产者生产

import threading
import queue
import time
import random
from decimal import Decimal

box=15
queuelist = queue.Queue(box)

class GoodsProduce(threading.Thread):
    def __init__(self,companyName,produceSpeed,info):
        super(GoodsProduce,self).__init__()
        self.companyName=companyName
        self.produceSpeed=Decimal(2/produceSpeed).quantize(Decimal('0.00'))
        self.info=info
    def run(self):
        while True:
           if not queuelist.full():
              time.sleep(self.produceSpeed)
              str="goods-%d"%random.randint(1,100)
              queuelist.put(str)
              print "GoodsProduce : %s create %s , now box have :%d" % (self.companyName,str, queuelist.qsize())
           else:
               print "NOTE: BOX is full , size %d ,filled %d" % (box, box)
               time.sleep(1)
    def show(self):
        print "companyName -- %s ,produceSpeed -- %s, infomation -- %s"%(self.companyName,self.produceSpeed,self.info)

class GoodsConsume(threading.Thread):
    def __init__(self,cname,area,info):
        super(GoodsConsume,self).__init__()
        self.cname=cname
        self.area=area
        self.info=info
    def run(self):
        while True:
            if not queuelist.empty():
                gname=queuelist.get()
                print "GoodsConsumer %s get %s , now box have :%d" % (self.cname,gname, queuelist.qsize())
            else :
                time.sleep(1)
                if  queuelist.empty():
                   print "NOTE: BOX is null ,please wait ...  size %d ,fillin 0" % (box)

            time.sleep(2)
    def show(self):
        print "GoodsConsume %s area -- %s ,infomation -- %s"%(self.cname,self.area,self.info)


if __name__ == "__main__":
    for server_num in range(0, 2):
        server = GoodsProduce("Prd-%d"%server_num,server_num+1,"this is %d prd company"%server_num)
        server.start()
        server.show()

    for customer_num in range(0, 5):
        customer = GoodsConsume("cus-%d"%customer_num,"area-%d"%customer_num,"this is %d customer"%customer_num)
        customer.start()
        customer.show()

输出:

companyName -- Prd-0 ,produceSpeed -- 2.00, infomation -- this is 0 prd company
companyName -- Prd-1 ,produceSpeed -- 1.00, infomation -- this is 1 prd company
GoodsConsume cus-0 area -- area-0 ,infomation -- this is 0 customer
GoodsConsume cus-1 area -- area-1 ,infomation -- this is 1 customer
GoodsConsume cus-2 area -- area-2 ,infomation -- this is 2 customer
GoodsConsume cus-3 area -- area-3 ,infomation -- this is 3 customer
GoodsConsume cus-4 area -- area-4 ,infomation -- this is 4 customer
GoodsProduce : Prd-1 create goods-16 , now box have :1
GoodsProduce : Prd-0 create goods-9 , now box have :2
GoodsProduce : Prd-1 create goods-44 , now box have :3
GoodsConsumer cus-1 get goods-16 , now box have :2
GoodsConsumer cus-2 get goods-9 , now box have :1
GoodsConsumer cus-0 get goods-44 , now box have :0
GoodsProduce : Prd-1 create goods-41 , now box have :1
GoodsConsumer cus-3 get goods-41 , now box have :0
GoodsProduce : Prd-0 create goods-61 , now box have :1
GoodsProduce : Prd-1 create goods-71 , now box have :2
GoodsConsumer cus-1 get goods-61 , now box have :1
GoodsConsumer cus-2 get goods-71 , now box have :0
NOTE: BOX is null ,please wait ...  size 15 ,fillin 0
Python 中,`queue` 模块提供了线程安全的队列实现,非常适合用于实现生产者-消费者模型。这种模型通过队列作为共享缓冲区,协调生产者消费者之间的数据交换。以下是基于多线程的实现示例: ### 示例代码:使用 `queue.Queue` 实现生产者-消费者模式 ```python import threading import time import random import queue # 生产者函数 def producer(queue, stop_flag): for _ in range(10): # 生产10个元素 item = random.randint(1, 100) print(f"生产者正在生产: {item}") queue.put(item) # 将生产的数据放入队列 time.sleep(random.uniform(0.1, 0.5)) # 模拟生产时间 queue.put(stop_flag) # 发送结束信号 # 消费者函数 def consumer(queue, stop_flag): while True: item = queue.get() # 从队列中取数据 if item == stop_flag: # 如果取到结束信号,退出循环 break print(f"消费者正在消费: {item}") time.sleep(random.uniform(0.1, 0.5)) # 模拟消费时间 queue.task_done() # 告诉队列当前任务处理完毕 if __name__ == "__main__": # 创建一个先进先出队列 shared_queue = queue.Queue() # 定义结束信号 STOP_FLAG = -1 # 创建生产者线程 producer_thread = threading.Thread(target=producer, args=(shared_queue, STOP_FLAG)) # 创建消费者线程 consumer_thread = threading.Thread(target=consumer, args=(shared_queue, STOP_FLAG)) # 启动生产者消费者线程 producer_thread.start() consumer_thread.start() # 等待生产者线程完成 producer_thread.join() # 等待消费者线程完成 consumer_thread.join() print("生产与消费过程结束。") ``` ### 代码说明 1. **队列创建**:使用 `queue.Queue()` 创建一个线程安全的队列对象,用于生产者消费者之间的通信。 2. **生产者函数**:模拟生产数据的过程,将数据放入队列中,并在完成生产后发送一个结束信号(`STOP_FLAG`)。 3. **消费者函数**:从队列中取出数据进行处理,如果检测到结束信号,则退出循环。 4. **线程管理**:启动生产者消费者线程,并使用 `join()` 等待它们完成任务。 ### 多进程场景 如果需要在多进程环境中实现生产者-消费者模型,可以使用 `multiprocessing.Queue` 代替 `queue.Queue`,同时将线程替换为进程[^2]。 ### 优势 - **线程安全**:`queue.Queue` 内部已经处理了锁机制,确保多个线程访问队列时的数据完整性[^3]。 - **简单易用**:通过 `put()` 和 `get()` 方法即可实现生产与消费操作,无需额外处理同步问题。 ### 注意事项 - **结束信号**:在队列末尾添加一个特殊标记(如 `None` 或自定义的结束信号),用于通知消费者线程任务完成。 - **性能优化**:可以根据实际需求调整队列的大小,避免内存占用过高或生产者/消费者速度不匹配的问题。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值