Created
July 18, 2014 15:55
-
-
Save luhn/98d18dca00edae2af93a to your computer and use it in GitHub Desktop.
txpostgres connection function (With automatic reconnect)
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 psycopg2.extras import DictCursor | |
from txpostgres import txpostgres | |
from txpostgres.reconnection import DeadConnectionDetector | |
def db_pool_from_config(config): | |
""" | |
Create a connection pool. Returns a deferred that will fire when the | |
connection pool is ready. | |
""" | |
def connection_factory(self, *args, **kwargs): | |
kwargs['detector'] = DeadConnectionDetector() | |
return txpostgres.Connection(*args, **kwargs) | |
txpostgres.ConnectionPool.connectionFactory = connection_factory | |
return txpostgres.ConnectionPool( | |
None, | |
database=config.get('database'), | |
user=config.get('user'), | |
password=config.get('password'), | |
host=config.get('host'), | |
cursor_factory=DictCursor, | |
min=config.get('num_conn', 5), | |
).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment