【问题标题】:asyncpg Connection pool with aiohttp raise asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress带有 aiohttp 的 asyncpg 连接池引发 asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress
【发布时间】:2022-10-15 05:28:39
【问题描述】:

当我尝试使用文档中的 aiohttp 重现 example 时 我得到错误:

asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress

当应用程序在任何添加的路由上获取 GET 请求时引发异常。

这是我的代码:

import asyncio
import asyncpg
from aiohttp import web


async def handle(request):
    """Handle incoming requests."""
    pool = request.app['pool']
    power = int(request.match_info.get('power', 10))

    # Take a connection from the pool.
    async with pool.acquire() as connection:
        # Open a transaction.
        async with connection.transaction():
            # Run the query passing the request argument.
            result = await connection.fetchval('select 2 ^ $1', power)
            return web.Response(
                text="2 ^ {} is {}".format(power, result))


async def init_app():
    """Initialize the application server."""
    app = web.Application()
    # Create a database connection pool
    app['pool'] = await asyncpg.create_pool(
        host="DB_HOST",
        port=5432,
        user="DB_USER",
        password="DB_PASSWORD",
        database="DB_NAME",)
    # Configure service routes
    app.router.add_route('GET', '/issues', handle)
    app.router.add_route('GET', '/', handle)
    return app


loop = asyncio.get_event_loop()
app = loop.run_until_complete(init_app())
web.run_app(app, port=8800)

如果我使用 PgBouncer,我会得到相同的结果

【问题讨论】:

    标签: python postgresql asyncpg connection-pool


    【解决方案1】:

    我找到了一个解决方案:

    它需要在run_app 方法中将循环作为参数传递

    ...
    web.run_app(app, port=8800, loop=loop)
    

    【讨论】:

      猜你喜欢
      • 2022-12-27
      • 2022-12-28
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 2013-03-19
      • 2022-12-02
      • 2020-09-28
      • 1970-01-01
      相关资源
      最近更新 更多