Added with-statement to pools.Pool objects for mad 2.5-ability. Should be 2.4-compatible though.
This commit is contained in:
@@ -91,6 +91,30 @@ class Pool(object):
|
|||||||
return created
|
return created
|
||||||
return self.channel.wait()
|
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):
|
def put(self, item):
|
||||||
"""Put an item back into the pool, when done
|
"""Put an item back into the pool, when done
|
||||||
"""
|
"""
|
||||||
@@ -140,7 +164,3 @@ class TokenPool(Pool):
|
|||||||
return Token()
|
return Token()
|
||||||
|
|
||||||
|
|
||||||
class ExceptionWrapper(object):
|
|
||||||
def __init__(self, e):
|
|
||||||
self.e = e
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user