-
-
Save Kroisse/5647881 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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