Python psycopg2.pool.ThreadedConnectionPool() Examples

The following are 6 code examples of psycopg2.pool.ThreadedConnectionPool(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module psycopg2.pool , or try the search function .
Example #1
Source File: connection_pool.py    From selene-backend with GNU Affero General Public License v3.0 5 votes vote down vote up
def allocate_db_connection_pool(
        connection_config: DatabaseConnectionConfig,
        max_connections: int = 20
) -> ThreadedConnectionPool:
    """
    Allocate a pool of database connections for an application

    Connecting to a database can be a costly operation for stateless
    applications that jump in and out of a database frequently,
    like a REST APIs. To combat this, a connection pool provides a set of
    persistent connections that preclude these applications from constantly
    connecting and disconnecting from the database.

    :param connection_config: data needed to establish a connection
    :param max_connections: maximum connections allocated to the application
    :return: a pool of database connections to be used by the application
    """
    log_msg = (
        'Allocating a pool of connections to the {db_name} database with '
        'a maximum of {max_connections} connections.'
    )
    _log.info(log_msg.format(
        db_name=connection_config.db_name,
        max_connections=max_connections)
    )
    return ThreadedConnectionPool(
        minconn=1,
        maxconn=max_connections,
        database=connection_config.db_name,
        user=connection_config.user,
        password=connection_config.password,
        host=connection_config.host,
        port=connection_config.port,
        cursor_factory=RealDictCursor
    ) 
Example #2
Source File: parse_output.py    From rgz_rcnn with MIT License 5 votes vote down vote up
def _setup_db_pool():
    from psycopg2.pool import ThreadedConnectionPool
    return ThreadedConnectionPool(1, 3, database='chen', user='chen') 
Example #3
Source File: database.py    From lopocs with GNU Lesser General Public License v2.1 5 votes vote down vote up
def init_app(cls, app):
        """
        Initialize db session lazily
        """
        query_con = ("postgresql://{PG_USER}:{PG_PASSWORD}@{PG_HOST}:"
                     "{PG_PORT}/{PG_NAME}"
                     .format(**app.config))
        cls.pool = ThreadedConnectionPool(1, cpu_count(), query_con)
        # keep some configuration element
        cls.dbname = app.config["PG_NAME"] 
Example #4
Source File: db_handler.py    From vmaas with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, size, dsn=None):
        if dsn:
            self.db_pool = pool.ThreadedConnectionPool(1, size,
                                                       dbname=dsn["database"], user=dsn["user"],
                                                       host=dsn["host"], port=dsn["port"])
        else:
            self.db_pool = pool.ThreadedConnectionPool(1, size,
                                                       dbname=VMAAS_DB_NAME, user=VMAAS_DB_LOGIN,
                                                       password=VMAAS_DB_PASSWD,
                                                       host=VMAAS_DB_HOST, port=VMAAS_DB_PORT) 
Example #5
Source File: app.py    From aerial_wildlife_detection with MIT License 5 votes vote down vote up
def _createConnectionPool(self):
        self.connectionPool = ThreadedConnectionPool(
            1,
            self.config.getProperty('Database', 'max_num_connections', type=int, fallback=20),
            host=self.host,
            database=self.database,
            port=self.port,
            user=self.user,
            password=self.password,
            connect_timeout=2
        ) 
Example #6
Source File: htc_api.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def setup_pool():
    global pool
    with open('htc_login.txt') as f:
      #each of these is expected to appear on  a separate line
      host = f.readline().rstrip()
      port = f.readline().rstrip()
      db   = f.readline().rstrip()
      user = f.readline().rstrip()
      pw   = f.readline().rstrip()
      pool = pgp.ThreadedConnectionPool(20, 100, host=host, port=port, database=db, user=user, password=pw)


#get current db connection if holding one, otherwise get a new one from the pool