Pool is a base class that implements resource limitation and construction. It is meant to be subclassed. When subclassing, define only the create() method to implement the desired resource:
class MyPool(pools.Pool):
def create(self):
return MyObject()
If using 2.5 or greater, the item() method acts as a context manager; that’s the best way to use it:
with mypool.item() as thing:
thing.dostuff()
If stuck on 2.4, the get() and put() methods are the preferred nomenclature. Use a finally to ensure that nothing is leaked:
thing = self.pool.get()
try:
thing.dostuff()
finally:
self.pool.put(thing)
The maximum size of the pool can be modified at runtime via the resize() method.
Specifying a non-zero min-size argument pre-populates the pool with min_size items. max-size sets a hard limit to the size of the pool – it cannot contain any more items than max_size, and if there are already max_size items ‘checked out’ of the pool, the pool will cause any greenthread calling get() to cooperatively yield until an item is put() in.
Generate a new pool item. This method must be overridden in order for the pool to function. It accepts no arguments and returns a single instance of whatever thing the pool is supposed to contain.
In general, create() is called whenever the pool exceeds its previous high-water mark of concurrently-checked-out-items. In other words, in a new pool with min_size of 0, the very first call to get() will result in a call to create(). If the first caller calls put() before some other caller calls get(), then the first item will be returned, and create() will not be called a second time.
Get an object out of the pool, for use with with statement.
>>> from eventlet import pools
>>> pool = pools.TokenPool(max_size=4)
>>> with pool.item() as obj:
... print "got token"
...
got token
>>> pool.free()
4
Resize the pool to new_size.
Adjusting this number does not affect existing items checked out of the pool, nor on any greenthreads who are waiting for an item to free up. Some indeterminate number of get()/put() cycles will be necessary before the new maximum size truly matches the actual operation of the pool.