python缓冲池

python
is 比较的是地址
== 比较的是内容

通常来说 dict list tuple str set int str …有很多都是对象 声明的时候内容可以一样但是 地址不会一样
所以判断的时候 is都为false 而==则为True

对象缓冲池
自己目前了解int str 有
str
python有个intern机制,简单说就是维护一个字典,这个字典维护已经创建字符串(key)和它的字符串对象的地址(value),每次创建字符串对象都会和这个字典比较,没有就创建,重复了就用指针进行引用就可以了。intern机制处理字符串长度小于等于20且仅由数字字母下划线构成的,只创建一次。
在源文件中 str is操作符返回值为True
在 input 时候会为 两个内容相同的字符串 is操作符返回 false
重点来了 但是在input时候如果字符串为 [a-z , A-Z , 0-9, _ ,] 中的一个这些内容中的一个的时候 is会为 true

int类型
>>> i = -5
>>> j = -5
>>> i is j # i和j是同一个对象
True
>>> i = 256
>>> j = 256
>>> i is j # i和j是同一个对象
True
>>> i = 257
>>> j = 257
>>> i is j # i和j是不同对象
False
由上面的实例可以看到,当变量在[-5,256]之间时,两个值相同的变量事实上会引用到同一个小整数对象上,也就是小整数对象池 中的对象,而不会去创建两个对象。而当变量超出了这个范围,两个值相同的变量也会各自创建整数对象,所以两者对应的对象不同

小整数对象池和字符缓冲池都是作用于运行过程中,python缓存小的整数和字符,当有变量使用这些对象时,不用额外创建对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一个Python实现的有界缓冲池,满足您的需求。代码如下所示: ```python import threading import queue import time class BoundedBuffer: def __init__(self, maxsize): self.maxsize = maxsize self.buffer = queue.Queue(maxsize) self.mutex = threading.Lock() self.empty = threading.Condition(self.mutex) self.full = threading.Condition(self.mutex) def put(self, item): with self.full: while self.buffer.full(): self.full.wait() self.buffer.put(item) self.empty.notify() def get(self): with self.empty: while self.buffer.empty(): self.empty.wait() item = self.buffer.get() self.full.notify() return item def get_buffer_info(self): with self.mutex: return {'size': self.buffer.qsize(), 'maxsize': self.maxsize} class Producer(threading.Thread): def __init__(self, buffer): threading.Thread.__init__(self) self.buffer = buffer self.stop_event = threading.Event() def run(self): while not self.stop_event.is_set(): item = time.time() # 生产一个元素 self.buffer.put(item) print(f'生产者 {self.name} 生产了一个元素 {item}') time.sleep(1) def stop(self): self.stop_event.set() class Consumer(threading.Thread): def __init__(self, buffer): threading.Thread.__init__(self) self.buffer = buffer self.stop_event = threading.Event() def run(self): while not self.stop_event.is_set(): item = self.buffer.get() print(f'消费者 {self.name} 消费了一个元素 {item}') time.sleep(1) def stop(self): self.stop_event.set() if __name__ == '__main__': buffer = BoundedBuffer(5) producers = [Producer(buffer) for i in range(2)] consumers = [Consumer(buffer) for i in range(3)] for p in producers: p.start() for c in consumers: c.start() while True: try: time.sleep(5) print(f'缓冲池状态: {buffer.get_buffer_info()}') except KeyboardInterrupt: print('停止生产者和消费者线程...') for p in producers: p.stop() for c in consumers: c.stop() break ``` 在上面的代码中,我们首先定义了一个 `BoundedBuffer` 类,这个类封装了一个有界缓冲池,并提供了 `put` 和 `get` 方法,用于往缓冲池中放入和取出元素。缓冲池的实现使用了 Python 的 `queue.Queue` 类,这个类本身就是线程安全的,所以不需要再使用锁进行同步。 在 `BoundedBuffer` 类中,我们使用了三个锁:`mutex`、`empty` 和 `full`。其中,`mutex` 是用于保护缓冲池的数据结构,`empty` 和 `full` 分别是用于控制消费者和生产者的等待条件。当缓冲池为空时,消费者会等待 `empty` 条件变量,当缓冲池为满时,生产者会等待 `full` 条件变量。 然后,我们定义了 `Producer` 和 `Consumer` 两个类,分别表示生产者和消费者线程。这两个类都继承自 `threading.Thread` 类,并实现了 `run` 方法。在 `run` 方法中,生产者会不断地向缓冲池中放入元素,而消费者会不断地从缓冲池中取出元素。这里我们使用了一个 `stop_event` 变量,用于控制线程的停止。 最后,在主程序中,我们创建了两个生产者和三个消费者线程,并启动它们。同时,我们也启动了一个循环,每隔一段时间就打印出缓冲池的状态。当用户按下 Ctrl+C 时,程序会捕捉到 `KeyboardInterrupt` 异常,然后停止生产者和消费者线程,退出程序。 综上所述,这个程序实现了有界缓冲池的基本功能,包括线程的同步和互斥、异常处理、状态显示等。您可以根据需要进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值