Added with-statement to pools.Pool objects for mad 2.5-ability. Should be 2.4-compatible though.

This commit is contained in:
Ryan Williams
2009-10-03 22:38:43 -07:00
parent 5bc7e88083
commit 4eb5e05a32

View File

@@ -90,7 +90,31 @@ class Pool(object):
self.current_size += 1
return created
return self.channel.wait()
try:
from contextlib import contextmanager
@contextmanager
def item(self):
""" 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
"""
obj = self.get()
try:
yield obj
finally:
self.put(obj)
except ImportError:
pass
def put(self, item):
"""Put an item back into the pool, when done
"""
@@ -140,7 +164,3 @@ class TokenPool(Pool):
return Token()
class ExceptionWrapper(object):
def __init__(self, e):
self.e = e