本文整理汇总了Python中psycopg2.pool.ThreadedConnectionPool方法的典型用法代码示例。如果您正苦于以下问题:Python pool.ThreadedConnectionPool方法的具体用法?Python pool.ThreadedConnectionPool怎么用?Python pool.ThreadedConnectionPool使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psycopg2.pool
的用法示例。
在下文中一共展示了pool.ThreadedConnectionPool方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: allocate_db_connection_pool
# 需要导入模块: from psycopg2 import pool [as 别名]
# 或者: from psycopg2.pool import ThreadedConnectionPool [as 别名]
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
)
示例2: _setup_db_pool
# 需要导入模块: from psycopg2 import pool [as 别名]
# 或者: from psycopg2.pool import ThreadedConnectionPool [as 别名]
def _setup_db_pool():
from psycopg2.pool import ThreadedConnectionPool
return ThreadedConnectionPool(1, 3, database='chen', user='chen')
示例3: init_app
# 需要导入模块: from psycopg2 import pool [as 别名]
# 或者: from psycopg2.pool import ThreadedConnectionPool [as 别名]
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"]
示例4: __init__
# 需要导入模块: from psycopg2 import pool [as 别名]
# 或者: from psycopg2.pool import ThreadedConnectionPool [as 别名]
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)
示例5: _createConnectionPool
# 需要导入模块: from psycopg2 import pool [as 别名]
# 或者: from psycopg2.pool import ThreadedConnectionPool [as 别名]
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
)
示例6: setup_pool
# 需要导入模块: from psycopg2 import pool [as 别名]
# 或者: from psycopg2.pool import ThreadedConnectionPool [as 别名]
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