Last active
March 23, 2018 11:35
-
-
Save eungju/5642496 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.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks good.