does python have a generic object pool like commons-pool in Java

Diez B. Roggisch deets at nospam.web.de
Wed Jul 15 08:40:05 EDT 2009


Rick Lawson wrote:

> On Jul 15, 4:53 am, Jonathan Gardner <jgard... at jonathangardner.net>
> wrote:
>> On Jul 14, 6:34 pm, Rick Lawson <lawso... at gmail.com> wrote:
>>
>> > Appreciate any help on this. I am porting an app from Java to python
>> > and need generic object pooling with hooks for object initialization /
>> > cleanup and be able to specify an object timeout.
>>
>> Are you looking for something like a thread pool or a connection pool?
>> Such a thing is easy to code in Python. Might as well write one from
>> scratch for your particular need.
>>
>> By the way, a tip for writing Python: Forget Java. If you're porting
>> an app, consider rewriting it from the architecture on up. You'll save
>> yourself time and get a better result.
> 
> Jonathan,
> 
> Thanks for the advice but would like to pick your brain a little more.
> The pool I need is not a thread pool or db pool. I need to pool
> objects which are a proxy for an external C process that communicates
> via sockets. The pool is necessary because the initial connection/
> authentication is expensive, plus the connection needs to be recycled
> every so often due to instability on the C side. The existing Java
> code uses commons-pool which incidentally is the base object pool for
> a db connection pooling library (DBCP). Anyway, maybe this is just so
> easy to do in Python that everyone rolls their own - but I have done a
> lot of googling with no luck.
> 
> I just hate to re-invent the wheel here - I guess I should look at a
> db connection pooling library and see if they have a generic object
> pool somewhere in there. Any you can recommend that are more or less
> standalone db connection pool libraries ?

I don't know enough about your domain, e.g. if your pooled objects have
state or not.

Assuming that 

 - they are objects
 - they don't have state (like database connections)
 - you want to recycle them after so many calls

one of the simplest solutions would be something like this:

class AutoDestructingWrapper(object):

   MAX_CALL_COUNT = 1000

   def __init__(self, object_factory):
       self._object_factory = object_factory
       self._call_count = self.MAX_CALL_COUNT


   def __getattr__(self, name):
       if self._call_count >= self.MAX_CALL_COUNT:
          self._delegate = self._object_factory()
       return getattr(self._delegate, name)


Diez



More information about the Python-list mailing list