threading Thread Queue 实现生产者消费者问题

[了解“生产者/消费者”模型]

在餐馆吃饭时,厨师做完一道菜后就会把菜从传菜窗口递出去,然后继续做下一道菜。厨师不需要关心顾客是不是已经把菜吃完了。如果厨师做菜的速度大于顾客拿菜的速度,那么就会有越来越多的菜堆在传菜窗口。如果顾客拿菜的速度大于厨师做菜的速度,那么传菜窗口始终是空的,来一道菜就会立刻被拿走。

在程序开发中,这就是一个典型的“生产者/消费者”模型:厨师是生产者,负责生产;顾客是消费者,负责消费。厨师和顾客各做各的事情。传菜窗口就是队列,它把生产者与消费者联系在一起。

[使用Python实现队列]

实例描述

使用Python自带的queue对象来实现队列:

(1)使用Python实现一个简单的“生产者/消费者”模型。

(2)使用Python的queue对象做信息队列。

在Python使用多线程实现生产者与消费者的程序中,可以使用Python自带的queue对象来作为生产者与消费者沟通的队列。

在代码2-1中,生产者负责产生两个数字,消费者负责把两个数字相加。

代码2-1 简单的“生产者/消费者”队列

Python
# 微信公众号zeroPython import time import <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/threading" title="View all posts in threading" target="_blank">threading</a></span> import random from <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/queue" title="View all posts in queue" target="_blank">queue</a></span> import Queue class Producter(<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/threading" title="View all posts in threading" target="_blank">threading</a></span>.Thread): def __init__(self, queue): super(Producter, self).__init__() self.queue = queue def run(self) -> None: while True: a = random.randint(1, 100) b = random.randint(1, 100) print(f"生产者产生了两个数字 {a},{b}") self.queue.put((a, b)) time.sleep(2) class Consumer(<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/thread" title="View all posts in thread" target="_blank">thread</a></span>ing.Thread): def __init__(self, queue): super(Consumer, self).__init__() self.queue = queue def run(self) -> None: while True: numble_tuple = self.queue.get(block=True) # 如果队列为空,则阻塞直至队列含有数据 print(f"开始消费 {numble_tuple}") time.sleep(random.randint(1,4)) queue = Queue() p = Producter(queue) c = Consumer(queue) p.start() c.start() # while True: # time.sleep(2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 微信公众号zeroPython
import time
import threading
import random
from queue import Queue
 
 
class Producter ( threading . Thread ) :
 
     def __init__ ( self , queue ) :
         super ( Producter , self ) . __init__ ( )
         self . queue = queue
 
     def run ( self ) -> None :
         while True :
             a = random . randint ( 1 , 100 )
             b = random . randint ( 1 , 100 )
 
             print ( f "生产者产生了两个数字 {a},{b}" )
 
             self . queue . put ( ( a , b ) )
             time . sleep ( 2 )
 
 
class Consumer ( threading . Thread ) :
     def __init__ ( self , queue ) :
         super ( Consumer , self ) . __init__ ( )
         self . queue = queue
 
     def run ( self ) -> None :
         while True :
             numble_tuple = self . queue . get ( block = True )
             # 如果队列为空,则阻塞直至队列含有数据
 
             print ( f "开始消费 {numble_tuple}" )
 
             time . sleep ( random . randint ( 1 , 4 ) )
 
queue = Queue ( )
 
 
p    = Producter ( queue )
c = Consumer ( queue )
 
p . start ( )
c . start ( )
 
# while True:
#     time.sleep(2)
 

代码2-1的运行结果存在两种情况:

● 如果消费者每次暂停的时间都小于2秒,那么队列始终是空的,来一组数立刻就被消费。

● 如果消费者每次暂停的时间都大于2秒,那么队列里的数就会越来越多。

但是,由于消费者暂停时间是随机的,我们不能提前知道它每次会暂停多久。

假定程序运行了1小时,请问队列里有多少数据?

如果使用Python自带的队列,就会出现以上的疑问。因为开发者不能直接看到队列的长度。如果开发者一开始就考虑到“需要随时观察队列长度”这个需求,那么可以通过对代码做一些修改来实现。但如果一开始没有打算观察队列长度,仅仅是临时起意,那该怎么办?

如果不仅想看队列长度,还想看里面每一组数都是什么,又该如何操作?

假设队列里已经堆积了一百组数,现在想增加消费者,该怎么增加?

如再运行一个Python程序,那能去读第一个正在运行中的Python程序中的队列吗?

Python 把队列中的数据存放在内存中。如果电脑突然断电,那队列里的数是不是全都丢失了?

为了防止丢数据,是否需要把数据持久化到硬盘?那持久化的代码怎么写,代码量有多少,考不考虑并发和读写冲突?




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值