Skip to content

Instantly share code, notes, and snippets.

@Kroisse
Forked from eungju/pool.py
Last active December 17, 2015 17:39
Show Gist options
  • Select an option

  • Save Kroisse/5647881 to your computer and use it in GitHub Desktop.

Select an option

Save Kroisse/5647881 to your computer and use it in GitHub Desktop.
class KyotoTycoon(object):
...
def get(self, key):
with self.pool.acquire() as c:
return c.get(key, db=self.db)
# example #2
def set(self, key, value):
conn = self.pool.acquire()
with conn:
return conn.set(key, value, db=self.db)
class ConnectionPool(object):
...
def acquire(self):
conn = self.pick_appropriate_connection()
conn.pool = self
return conn
class Connection(...):
...
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
if exc_type is None:
self.pool.release(self)
else:
# isinstance() can receive a tuple of types
if self.exc_types and isinstance(exc_value, tuple(self.exc_types))):
self.pool.abandon(self)
else:
self.pool.release(self)
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment