Skip to content

Instantly share code, notes, and snippets.

@eungju
Last active March 23, 2018 11:35
Show Gist options
  • Save eungju/5642496 to your computer and use it in GitHub Desktop.
Save eungju/5642496 to your computer and use it in GitHub Desktop.
class KyotoTycoon(object):
...
def get(self, key):
with self.pool.context() as c:
return c.get(key, db=self.db)
class ConnectionPool(object):
...
def context(self):
return ConnectionGuard(self)
class ConnectionGuard(object):
def __init__(self, pool):
self.pool = pool
def __enter__(self):
self.connection = self.pool.acquire()
return self.connection
def __exit__(self, exc_type, exc_value, traceback):
if exc_type is None:
self.pool.release(self.connection)
else:
if self.connection.exc_types and any(
isinstance(exc_value, exc_type) for exc_type in self.connection.exc_types):
self.pool.abandon(self.connection)
else:
self.pool.release(self.connection)
return False
@iasandcb
Copy link

Looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment