本文整理汇总了Python中sqlalchemy.pool.Pool方法的典型用法代码示例。如果您正苦于以下问题:Python pool.Pool方法的具体用法?Python pool.Pool怎么用?Python pool.Pool使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.pool
的用法示例。
在下文中一共展示了pool.Pool方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_database_engine
# 需要导入模块: from sqlalchemy import pool [as 别名]
# 或者: from sqlalchemy.pool import Pool [as 别名]
def get_database_engine(
config: Config, poolclass: Optional[Type[Pool]] = None,
) -> Engine:
try:
engine = config.DATABASE_ENGINE
except AttributeError:
url = config.DATABASE_URL
echo = config.DATABASE_ECHO
engine = create_database_engine(url, echo, poolclass)
else:
if engine is None:
url = config.DATABASE_URL
echo = config.DATABASE_ECHO
engine = create_database_engine(url, echo, poolclass)
return engine
示例2: test_rec_close_reopen
# 需要导入模块: from sqlalchemy import pool [as 别名]
# 或者: from sqlalchemy.pool import Pool [as 别名]
def test_rec_close_reopen(self):
# test that _ConnectionRecord.close() allows
# the record to be reusable
dbapi = MockDBAPI()
p1 = pool.Pool(creator=lambda: dbapi.connect("foo.db"))
r1 = pool._ConnectionRecord(p1)
c1 = r1.connection
c2 = r1.get_connection()
is_(c1, c2)
r1.close()
assert not r1.connection
eq_(c1.mock_calls, [call.close()])
c2 = r1.get_connection()
is_not_(c1, c2)
is_(c2, r1.connection)
eq_(c2.mock_calls, [])
示例3: test_listen_targets_scope
# 需要导入模块: from sqlalchemy import pool [as 别名]
# 或者: from sqlalchemy.pool import Pool [as 别名]
def test_listen_targets_scope(self):
canary = []
def listen_one(*args):
canary.append("listen_one")
def listen_two(*args):
canary.append("listen_two")
def listen_three(*args):
canary.append("listen_three")
def listen_four(*args):
canary.append("listen_four")
engine = testing_engine(testing.db.url)
event.listen(pool.Pool, "connect", listen_one)
event.listen(engine.pool, "connect", listen_two)
event.listen(engine, "connect", listen_three)
event.listen(engine.__class__, "connect", listen_four)
engine.execute(select([1])).close()
eq_(
canary, ["listen_one", "listen_four", "listen_two", "listen_three"]
)
示例4: connection_event
# 需要导入模块: from sqlalchemy import pool [as 别名]
# 或者: from sqlalchemy.pool import Pool [as 别名]
def connection_event():
@event.listens_for(Pool, "checkout")
def ping_connection(dbapi_connection, connection_record, connection_proxy):
# 在每次从连接池获取一个连接时,首先测试连接池是否畅通
# 如果不畅通,则断开重新建立连接,及时防丢
cursor = dbapi_connection.cursor()
try:
cursor.execute("SELECT 1")
except:
SysLogger.error('database pool has gone away')
connection_proxy._pool.dispose()
cursor.close()
示例5: create_database_engine
# 需要导入模块: from sqlalchemy import pool [as 别名]
# 或者: from sqlalchemy.pool import Pool [as 别名]
def create_database_engine(
url: str, echo: bool, poolclass: Optional[Type[Pool]] = None,
) -> Engine:
return create_engine(
url, echo=echo, poolclass=poolclass, pool_pre_ping=True,
)
示例6: test_rec_unconnected
# 需要导入模块: from sqlalchemy import pool [as 别名]
# 或者: from sqlalchemy.pool import Pool [as 别名]
def test_rec_unconnected(self):
# test production of a _ConnectionRecord with an
# initially unconnected state.
dbapi = MockDBAPI()
p1 = pool.Pool(creator=lambda: dbapi.connect("foo.db"))
r1 = pool._ConnectionRecord(p1, connect=False)
assert not r1.connection
c1 = r1.get_connection()
is_(c1, r1.connection)
示例7: test_listen_targets_per_subclass
# 需要导入模块: from sqlalchemy import pool [as 别名]
# 或者: from sqlalchemy.pool import Pool [as 别名]
def test_listen_targets_per_subclass(self):
"""test that listen() called on a subclass remains specific to
that subclass."""
canary = []
def listen_one(*args):
canary.append("listen_one")
def listen_two(*args):
canary.append("listen_two")
def listen_three(*args):
canary.append("listen_three")
event.listen(pool.Pool, "connect", listen_one)
event.listen(pool.QueuePool, "connect", listen_two)
event.listen(pool.SingletonThreadPool, "connect", listen_three)
p1 = pool.QueuePool(creator=MockDBAPI().connect)
p2 = pool.SingletonThreadPool(creator=MockDBAPI().connect)
assert listen_one in p1.dispatch.connect
assert listen_two in p1.dispatch.connect
assert listen_three not in p1.dispatch.connect
assert listen_one in p2.dispatch.connect
assert listen_two not in p2.dispatch.connect
assert listen_three in p2.dispatch.connect
p1.connect()
eq_(canary, ["listen_one", "listen_two"])
p2.connect()
eq_(canary, ["listen_one", "listen_two", "listen_one", "listen_three"])
示例8: teardown
# 需要导入模块: from sqlalchemy import pool [as 别名]
# 或者: from sqlalchemy.pool import Pool [as 别名]
def teardown(self):
# TODO: need to get remove() functionality
# going
pool.Pool.dispatch._clear()
示例9: create_engine_from_conf
# 需要导入模块: from sqlalchemy import pool [as 别名]
# 或者: from sqlalchemy.pool import Pool [as 别名]
def create_engine_from_conf(config):
need_connection_pool_fix = True
if not config.has_section('database'):
seafile_data_dir = os.environ['SEAFILE_CONF_DIR']
if seafile_data_dir:
path = os.path.join(seafile_data_dir, 'seafile.db')
else:
logging.warning('SEAFILE_CONF_DIR not set, can not load sqlite database.')
return None
db_url = "sqlite:///%s" % path
need_connection_pool_fix = False
else:
backend = config.get('database', 'type')
if backend == 'mysql':
if config.has_option('database', 'host'):
host = config.get('database', 'host').lower()
else:
host = 'localhost'
if config.has_option('database', 'port'):
port = config.getint('database', 'port')
else:
port = 3306
username = config.get('database', 'user')
passwd = config.get('database', 'password')
dbname = config.get('database', 'db_name')
db_url = "mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8" % (username, quote_plus(passwd), host, port, dbname)
elif backend == 'oracle':
if config.has_option('database', 'host'):
host = config.get('database', 'host').lower()
else:
host = 'localhost'
if config.has_option('database', 'port'):
port = config.getint('database', 'port')
else:
port = 1521
username = config.get('database', 'username')
passwd = config.get('database', 'password')
service_name = config.get('database', 'service_name')
db_url = "oracle://%s:%s@%s:%s/%s" % (username, quote_plus(passwd),
host, port, service_name)
else:
raise RuntimeError("Unknown database backend: %s" % backend)
# Add pool recycle, or mysql connection will be closed by mysqld if idle
# for too long.
kwargs = dict(pool_recycle=300, echo=False, echo_pool=False)
engine = create_engine(db_url, **kwargs)
if need_connection_pool_fix and not has_event_listener(Pool, 'checkout', ping_connection):
# We use has_event_listener to double check in case we call create_engine
# multipe times in the same process.
add_event_listener(Pool, 'checkout', ping_connection)
return engine