Last active
August 17, 2018 19:31
-
-
Save binarymachines/191656b24122f18540f260e5ab9f8017 to your computer and use it in GitHub Desktop.
Trying to solve the problem of using context managers with transaction pools
This file contains 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
from contextlib import contextmanager | |
from contextlib import ContextDecorator | |
import psycopg2 | |
from sqlalchemy import create_engine | |
import os | |
import uuid | |
class DBTask(object): | |
def __init__(self, name='anonymous DB task'): | |
self.name = name | |
self._id = None | |
def _get_uuid(self): | |
# can be overridden in descendant classes | |
return uuid.uuid4() | |
def __str__(self): | |
return 'dbtask[%s]:%s' % (self.name, self.uuid) | |
@property | |
def uuid(self): | |
if not self._id: | |
self._id = self._get_uuid() | |
return self._id | |
class ConnectionPoolManager(Object): | |
def __init__(self, hostname, db_name, username, password, port='5432'): | |
#self.connect_string = 'postgresql://'+user+':'+password+'@'+host+':'+port+'/'+dbname | |
#conn = create_engine(connect_string, pool_size=20, max_overflow_size=0) | |
self.hostname = hostname | |
self.dbname = db_name | |
self.username = username | |
self.password = password | |
self.port = port | |
self.connection_pool = pool.QueuePool(self._getconn, | |
max_overflow=10, | |
pool_size=5) | |
self.connection_table = {} | |
def _get_connection(self): # can override in subclasses | |
c = psycopg2.connect(username=self.username, | |
host=self.hostname, | |
dbname=self.dbname, | |
password=self.password, | |
port=self.port) | |
return c | |
def get_connection(self, db_task): | |
conn = self._get_connection() | |
self.connection_table[db_task.uuid()] = conn | |
return conn | |
@contextmanager | |
def get_connection(connection_pool_mgr, db_task): | |
conn = connection_mgr.get_connection(db_task) | |
yield conn | |
connection_mgr.close_connection(db_task) | |
cpmgr = ConnectionPoolManager('mydb', ...) | |
with get_connection(cpmgr, DBTask('update table')) as conn: | |
... | |
... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment