title: Python Implementation of the Object Pool Design Pattern
body: <p>I need an <a href="http://en.wikipedia.org/wiki/Object_pool" rel="noreferrer">Object Pool</a>, and rather than implement it myself, I thought I would look around for a ready-made and tested Python library.</p>
  
  <p>What I found was plenty of other people <a href="http://mail.python.org/pipermail/python-list/2009-July/719988.html" rel="noreferrer">looking</a>, but not getting many straight answers, so I have brought it over here to Stack Overflow.</p>
  
  <p>In my case, I have a large number of threads (using the <code>threading</code> 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.</p>
  
  <p>If the items to pool were worker subprocesses, I might have chosen <code>multiprocessing.pool</code>, but they are not. If they were worker threads, I might have chosen <a href="http://code.activestate.com/recipes/203871/" rel="noreferrer">this implementation</a>, but they are not.</p>
  
  <p>If they were MySQL connections, I might have chosen <a href="http://code.google.com/p/pysqlpool/" rel="noreferrer">pysqlpool</a>, but they are not. Similarly the <a href="http://www.sqlalchemy.org/docs/04/pooling.html" rel="noreferrer">SQLAlchemy Pool</a> is out.</p>
  
  <p>If there was one thread, using a variable number of connections/objects, I would consider <a href="http://www.devpicayune.com/entry/object-pooling-in-python" rel="noreferrer">this implementation</a>, but I need it to be thread-safe.</p>
  
  <p>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.</p>