python对象池模式
发布日期:2021-09-11 05:52:45 浏览次数:21 分类:技术文章

本文共 1205 字,大约阅读时间需要 4 分钟。

 

 

class QueueObject():    def __init__(self, queue, auto_get=False):        self._queue = queue        self.object = self._queue.get() if auto_get else None    def __enter__(self):        if self.object is None:            self.object = self._queue.get()        return self.object    def __exit__(self, Type, value, traceback):        if self.object is not None:            self._queue.put(self.object)            self.object = None    def __del__(self):        if self.object is not None:            self._queue.put(self.object)            self.object = Nonedef main():    try:        import queue    except ImportError:  # python 2.x的兼容性        import Queue as queue    def test_object(queue):        queue_object = QueueObject(queue, True)        print('内部 func: {}'.format(queue_object.object))    sample_queue = queue.Queue()    sample_queue.put('yam')    with QueueObject(sample_queue) as obj:        print('Inside with: {}'.format(obj))    print('Outside with: {}'.format(sample_queue.get()))    sample_queue.put('sam')    test_object(sample_queue)    print('外部 func: {}'.format(sample_queue.get()))    if not sample_queue.empty():        print(sample_queue.get())if __name__ == '__main__':    main()

 

转载地址:https://blog.csdn.net/weixin_34406061/article/details/86015904 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:爬虫开发python工具包介绍 (1)
下一篇:vagrant up 时提示错误 cound not open file

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年03月02日 01时13分15秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

辽宁师范大学java_辽宁师范大学心理学院 2019-04-21
java程序有连接数据库_Java程序连接数据库 2019-04-21
java reduce.mdn_reduce高级用法 2019-04-21
java shape用法_Java PShape.scale方法代码示例 2019-04-21
java字符串三目_java字符串连接运算符和三目运算符 2019-04-21
java 堆内存 非堆内存_JVM 堆内存和非堆内存 2019-04-21
Java新手写什么demo_通过入门demo简单了解netty使用方法 2019-04-21
java国际化bundle_java语言国际化--ResouceBundle、struts 2019-04-21
java图片延迟加载_jQuery实现图片延迟加载 2019-04-21
java开发加入购物车功能_java web开发——购物车功能实现 2019-04-21
Java虚拟机不能满足_深入理解Java虚拟机--读书笔记1/3 2019-04-21
python 协程 asyncio_python – asyncio.as_completed是否会产生期货或协同程序? 2019-04-21
java设定xml文件的encoding_配置web-xml解决中文乱码问题,及各种乱码问题集结 2019-04-21
hanlp java api_java分词工具hanlp介绍 2019-04-21
nginx php 源码安装,Nginx1.12.2加php7.2.0的编译安装 2019-04-21
php 删除字节,php – 删除无效/不完整的多字节字符 2019-04-21
php 实现版本号对比,如何在PHP中实现比较版本号 2019-04-21
php sql 给数据库追加内容,php如何向数据库中的某串数据后追加内容【急】 2019-04-21
php微信小程序获取用户信息,微信小程序授权获取用户详细信息openid的实例详解... 2019-04-21
Java三元运算和if,Java三元运算符与<JDK8兼容性中的if / else 2019-04-21