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


Python aiopg.create_pool方法代碼示例

本文整理匯總了Python中aiopg.create_pool方法的典型用法代碼示例。如果您正苦於以下問題:Python aiopg.create_pool方法的具體用法?Python aiopg.create_pool怎麽用?Python aiopg.create_pool使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在aiopg的用法示例。


在下文中一共展示了aiopg.create_pool方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: main

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def main():
    tornado.options.parse_command_line()

    # Create the global connection pool.
    async with aiopg.create_pool(
        host=options.db_host,
        port=options.db_port,
        user=options.db_user,
        password=options.db_password,
        dbname=options.db_database,
    ) as db:
        await maybe_create_tables(db)
        app = Application(db)
        app.listen(options.port)

        # In this demo the server will simply run until interrupted
        # with Ctrl-C, but if you want to shut down more gracefully,
        # call shutdown_event.set().
        shutdown_event = tornado.locks.Event()
        await shutdown_event.wait() 
開發者ID:DataDog,項目名稱:trace-examples,代碼行數:22,代碼來源:blog.py

示例2: _init

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def _init(self):

        host = os.environ.get("MF_METADATA_DB_HOST", "localhost")
        port = os.environ.get("MF_METADATA_DB_PORT", 5432)
        user = os.environ.get("MF_METADATA_DB_USER", "postgres")
        password = os.environ.get("MF_METADATA_DB_PSWD", "postgres")
        database_name = os.environ.get("MF_METADATA_DB_NAME", "postgres")

        dsn = "dbname={0} user={1} password={2} host={3} port={4}".format(
            database_name, user, password, host, port
        )
        # todo make poolsize min and max configurable as well as timeout
        # todo add retry and better error message
        retries = 3
        for i in range(retries):
            while True:
                try:
                    self.pool = await aiopg.create_pool(dsn)
                except Exception as e:
                    if retries - i < 1:
                        raise e
                    time.sleep(1)
                    continue
                break 
開發者ID:Netflix,項目名稱:metaflow-service,代碼行數:26,代碼來源:postgres_async_db.py

示例3: test_release_with_invalid_status

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def test_release_with_invalid_status(create_pool):
    pool = await create_pool(minsize=10)
    conn = await pool.acquire()
    assert 9 == pool.freesize
    assert {conn} == pool._used
    cur = await conn.cursor()
    await cur.execute('BEGIN')
    cur.close()

    with mock.patch("aiopg.pool.warnings") as m_log:
        pool.release(conn)
    assert 9 == pool.freesize
    assert not pool._used
    assert conn.closed
    m_log.warn.assert_called_with(
        "Invalid transaction status on "
        "released connection: {}".format(TRANSACTION_STATUS_INTRANS),
        ResourceWarning
    ) 
開發者ID:aio-libs,項目名稱:aiopg,代碼行數:21,代碼來源:test_pool.py

示例4: test_release_with_invalid_status_wait_release

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def test_release_with_invalid_status_wait_release(create_pool):
    pool = await create_pool(minsize=10)
    conn = await pool.acquire()
    assert 9 == pool.freesize
    assert {conn} == pool._used
    cur = await conn.cursor()
    await cur.execute('BEGIN')
    cur.close()

    with mock.patch("aiopg.pool.warnings") as m_log:
        await pool.release(conn)
    assert 9 == pool.freesize
    assert not pool._used
    assert conn.closed
    m_log.warn.assert_called_with(
        "Invalid transaction status on "
        "released connection: {}".format(TRANSACTION_STATUS_INTRANS),
        ResourceWarning
    ) 
開發者ID:aio-libs,項目名稱:aiopg,代碼行數:21,代碼來源:test_pool.py

示例5: test_fill_free

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def test_fill_free(create_pool, loop):
    pool = await create_pool(minsize=1)
    with (await pool):
        assert 0 == pool.freesize
        assert 1 == pool.size

        conn = await asyncio.wait_for(pool.acquire(),
                                      timeout=0.5,
                                      loop=loop)
        assert 0 == pool.freesize
        assert 2 == pool.size
        pool.release(conn)
        assert 1 == pool.freesize
        assert 2 == pool.size
    assert 2 == pool.freesize
    assert 2 == pool.size 
開發者ID:aio-libs,項目名稱:aiopg,代碼行數:18,代碼來源:test_pool.py

示例6: test_connection_in_good_state_after_timeout

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def test_connection_in_good_state_after_timeout(create_pool):
    async def sleep(conn):
        cur = await conn.cursor()
        await cur.execute('SELECT pg_sleep(10);')

    pool = await create_pool(minsize=1,
                             maxsize=1,
                             timeout=0.1)
    with (await pool) as conn:
        with pytest.raises(asyncio.TimeoutError):
            await sleep(conn)

    assert 1 == pool.freesize

    with (await pool) as conn:
        cur = await conn.cursor()
        await cur.execute('SELECT 1;')
        val = await cur.fetchone()
        assert (1,) == val 
開發者ID:aio-libs,項目名稱:aiopg,代碼行數:21,代碼來源:test_pool.py

示例7: test_pool_with_connection_recycling

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def test_pool_with_connection_recycling(create_pool, loop):
    pool = await create_pool(minsize=1,
                             maxsize=1,
                             pool_recycle=3)
    with (await pool) as conn:
        cur = await conn.cursor()
        await cur.execute('SELECT 1;')
        val = await cur.fetchone()
        assert (1,) == val

    await asyncio.sleep(5, loop=loop)

    assert 1 == pool.freesize
    with (await pool) as conn:
        cur = await conn.cursor()
        await cur.execute('SELECT 1;')
        val = await cur.fetchone()
        assert (1,) == val 
開發者ID:aio-libs,項目名稱:aiopg,代碼行數:20,代碼來源:test_pool.py

示例8: test_connection_in_good_state_after_timeout_in_transaction

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def test_connection_in_good_state_after_timeout_in_transaction(
        create_pool):
    async def sleep(conn):
        cur = await conn.cursor()
        await cur.execute('BEGIN;')
        await cur.execute('SELECT pg_sleep(10);')

    pool = await create_pool(minsize=1,
                             maxsize=1,
                             timeout=0.1)
    with (await pool) as conn:
        with pytest.raises(asyncio.TimeoutError):
            await sleep(conn)
        conn.close()

    assert 0 == pool.freesize
    assert 0 == pool.size
    with (await pool) as conn:
        async with conn.cursor() as cur:
            await cur.execute('SELECT 1;')
            val = await cur.fetchone()
            assert (1,) == val 
開發者ID:aio-libs,項目名稱:aiopg,代碼行數:24,代碼來源:test_pool.py

示例9: test_pool_on_connect

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def test_pool_on_connect(create_pool):
    called = False

    async def cb(connection):
        nonlocal called
        async with connection.cursor() as cur:
            await cur.execute('SELECT 1')
            data = await cur.fetchall()
            assert [(1,)] == data
            called = True

    pool = await create_pool(on_connect=cb)

    with (await pool.cursor()) as cur:
        await cur.execute('SELECT 1')

    assert called 
開發者ID:aio-libs,項目名稱:aiopg,代碼行數:19,代碼來源:test_pool.py

示例10: test_pool_context_manager_timeout

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def test_pool_context_manager_timeout(pg_params, loop):
    async with aiopg.create_pool(**pg_params, minsize=1,
                                 maxsize=1) as pool:
        cursor_ctx = await pool.cursor()
        with pytest.raises(psycopg2.ProgrammingError):
            with cursor_ctx as cursor:
                hung_task = cursor.execute('SELECT pg_sleep(10000);')
                # start task
                fut = loop.create_task(hung_task)
                # sleep for a bit so it gets going
                await asyncio.sleep(1)

        fut.cancel()
        cursor_ctx = await pool.cursor()
        with cursor_ctx as cursor:
            resp = await cursor.execute('SELECT 42;')
            resp = await cursor.fetchone()
            assert resp == (42,)

    assert cursor.closed
    assert pool.closed 
開發者ID:aio-libs,項目名稱:aiopg,代碼行數:23,代碼來源:test_async_await.py

示例11: try_connect_postgres

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def try_connect_postgres(self, host, user, password, database):
        """Loop forever and attempt to connect to postgres.

        Args:
            host -- the hostname to connect to.
            user -- the username to authenticate as.
            password -- the password to authenticate with.
            database -- the name of the database to use.

        Returns:
            The connected postgres client.
        """

        while True:
            logger.info("Trying to connect to postgres... {}@{}/{}".format(user, host, database))
            logger.debug("loop: {}".format(self.loop))
            try:
                postgres = await aiopg.create_pool(
                        loop=self.loop,
                        host=host,
                        user=user,
                        database=database,
                        password=password)
                logger.info("Successfully connected to postgres")
                return postgres
            except:
                logger.warn("Failed to connect to postgres")
                time.sleep(5) 
開發者ID:scrapbird,項目名稱:sarlacc,代碼行數:30,代碼來源:storage.py

示例12: run_aiopg

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def run_aiopg():
    pool = await aiopg.create_pool(dsn, minsize=5, maxsize=5)
    t0 = time.time()
    for i in range(1000):
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("SELECT 1")
                ret = []
                async for row in cur:
                    ret.append(row)
                assert ret == [(1,)]
    print('run_aiopg', time.time() - t0)
    pool.close()
    await pool.wait_closed() 
開發者ID:anyant,項目名稱:rssant,代碼行數:16,代碼來源:benchmark_asyncio_postgres.py

示例13: run_asyncpg

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def run_asyncpg():
    async with asyncpg.create_pool(
            user='rssant', password='rssant',
            database='rssant', host='127.0.0.1',
            command_timeout=60, min_size=5, max_size=5
    ) as pool:
        t0 = time.time()
        for i in range(1000):
            async with pool.acquire() as conn:
                values = await conn.fetch("SELECT 1")
                assert values == [(1,)]
        print('run_asyncpg', time.time() - t0)
    await pool.close() 
開發者ID:anyant,項目名稱:rssant,代碼行數:15,代碼來源:benchmark_asyncio_postgres.py

示例14: _init

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def _init(self):

        host = os.environ.get("MF_METADATA_DB_HOST", "localhost")
        port = os.environ.get("MF_METADATA_DB_PORT", 5432)
        user = os.environ.get("MF_METADATA_DB_USER", "postgres")
        password = os.environ.get("MF_METADATA_DB_PSWD", "postgres")
        database_name = os.environ.get("MF_METADATA_DB_NAME", "postgres")

        dsn = "dbname={0} user={1} password={2} host={3} port={4}".format(
            database_name, user, password, host, port
        )
        # todo make poolsize min and max configurable as well as timeout
        # todo add retry and better error message
        retries = 3
        for i in range(retries):
            while True:
                try:
                    self.pool = await aiopg.create_pool(dsn)
                    for table in self.tables:
                        await table._init()
                except Exception as e:
                    if retries - i < 1:
                        raise e
                    time.sleep(1)
                    continue
                break 
開發者ID:Netflix,項目名稱:metaflow-service,代碼行數:28,代碼來源:postgres_async_db.py

示例15: _connect

# 需要導入模塊: import aiopg [as 別名]
# 或者: from aiopg import create_pool [as 別名]
def _connect(self):
        conn_kwargs = {
            "database": self.database,
        }

        conn_kwargs.update(self.connect_params)
        if 'passwd' in conn_kwargs:
            conn_kwargs['password'] = conn_kwargs.pop('passwd')
        if "db" in conn_kwargs:
            conn_kwargs["database"] = conn_kwargs.pop("db")
        if "maxsize" not in conn_kwargs:
            conn_kwargs["maxsize"] = 32
        return aiopg.create_pool(None, **conn_kwargs) 
開發者ID:snower,項目名稱:torpeewee,代碼行數:15,代碼來源:postgresql.py


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