Python实现对象池设计模式 [英] Python Implementation of the Object Pool Design Pattern

查看:270
本文介绍了Python实现对象池设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个对象池,而不是自己实现,我以为我会看周围有一个现成的和经过测试的Python库。



我发现很多其他人,但没有得到很多直接的答案,所以我把它带到了这个堆栈溢出。



在我的情况下,我有大量的线程(使用线程模块),这需要偶尔调用远程基于SOAP的服务器。他们可以建立自己的连接到服务器,但设置一个套接字和完成身份验证过程是昂贵的(它受到服务器的限制),所以我想共享一个连接池,只需要根据需要创建。 p>

如果要池的项目是工作子进程,我可能选择了 multiprocessing.pool ,但不是。如果他们是工作线程,我可能会选择此实施,但不是。



如果是MySQL连接,我可能选择了 pysqlpool ,但不是。同样地, SQLAlchemy Pool 也是。



如果有一个线程,使用可变数量的连接/对象,我会考虑这个实现,但我需要它是线程安全的。



我知道我可以相当快地实现这一点,但是鉴于有许多人寻找它,我认为Stack Overflow的一个典型的答案是很好的。

解决方案

在我看来,从你的描述,你需要的是一个连接池,而不是对象。为了简单的线程安全,只需将可重用的连接保存在一个 Queue.Queue 实例中,就可以调用它 pool 。当一个线程实例化一个连接包装对象时,该对象通过 pool.get()(如果没有当前可用的连接和出库的连接自动排队等待它)连接当连接准备好了)当对象完成使用其连接时,它会通过 pool.put 将其放回池中。



有这样这个通用功能很少,除了 Queue.Queue 已经给你以外,没有什么模块提供它是众所周知的或流行的 - 并不奇怪当模块有大约6行的功能代码(例如,调用用户提供的连接工厂来提前或恰好在某个最大数量的时间内填充队列时,使模块广泛使用 - 不是一个大的添加价值一般,反正)。 厚胶,厚实地包装标准库模块的底层功能,没有实质的附加价值,毕竟是一个架构减号; - )。


I need an Object Pool, and rather than implement it myself, I thought I would look around for a ready-made and tested Python library.

What I found was plenty of other people looking, but not getting many straight answers, so I have brought it over here to Stack Overflow.

In my case, I have a large number of threads (using the threading module), which need to occasionally call a remote SOAP-based server. They could each establish their own connection to the server, but setting up a socket and completing the authentication process is expensive (it is throttled by the server), so I want to share a pool of connections, creating more only as needed.

If the items to pool were worker subprocesses, I might have chosen multiprocessing.pool, but they are not. If they were worker threads, I might have chosen this implementation, but they are not.

If they were MySQL connections, I might have chosen pysqlpool, but they are not. Similarly the SQLAlchemy Pool is out.

If there was one thread, using a variable number of connections/objects, I would consider this implementation, but I need it to be thread-safe.

I know I could implement this again fairly quickly, but given there are many people looking for it, I thought a canonical answer on Stack Overflow would be nice.

解决方案

It seems to me, from your description, that what you need is a pool of connections, not of objects. For simple thread-safety, just keep the reusable connections in a Queue.Queue instance, call it pool. When a thread instantiates a connection-wrapping object, the object gets its connection via pool.get() (which automaticaly enqueues it to wait if there are no connections currently availabe and dequeues it when a connection's ready for it); when the object's done using its connection, it puts it back in the pool via pool.put.

There's so little universally-required, general-purpose functionality in this, beyond what Queue.Queue already gives you, that it's not surprising no module providing it is well known or popular -- hard to make a module widespread when it has about 6 lines of functional code in all (e.g. to call a user-supplied connection factory to populate the queue either in advance or just-in-time up to some maximum number -- not a big added value generally, anyway). "Thick glue", thickly wrapping the underlying functionality from a standard library module without substantial added value, is an architectural minus, after all;-).

这篇关于Python实现对象池设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆