0

When I try to reproduce example with aiohttp from documentation I get the error:

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

Exception raises when app get GET request on any of added routes.

Here is my code:

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)

If i use PgBouncer I get the same result

Andrey Topoleov
  • 1,591
  • 15
  • 20

1 Answers1

0

I found a solution:

it needs to pass loop as argument in run_app method

...
web.run_app(app, port=8800, loop=loop)
Andrey Topoleov
  • 1,591
  • 15
  • 20