當前位置: 首頁>>代碼示例>>Python>>正文


Python pool.Pool方法代碼示例

本文整理匯總了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 
開發者ID:item4,項目名稱:yui,代碼行數:18,代碼來源:engine.py

示例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, []) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:25,代碼來源:test_pool.py

示例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"]
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:27,代碼來源:test_pool.py

示例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() 
開發者ID:mqingyn,項目名稱:torngas,代碼行數:14,代碼來源:dbalchemy.py

示例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,
    ) 
開發者ID:item4,項目名稱:yui,代碼行數:8,代碼來源:engine.py

示例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) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:14,代碼來源:test_pool.py

示例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"]) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:35,代碼來源:test_pool.py

示例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() 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:6,代碼來源:test_pool.py

示例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 
開發者ID:haiwen,項目名稱:seafobj,代碼行數:61,代碼來源:db.py


注:本文中的sqlalchemy.pool.Pool方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。