MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.88k stars 399 forks source link

Connection pool example with aiohttp from documentation raise asyncpg.exceptions._base.InterfaceError #966

Closed topoleov closed 1 year ago

topoleov commented 1 year ago

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

2022-10-12 16:58:42,613 [ERROR] aiohttp.server: Error handling request
Traceback (most recent call last):
File "/home/mario/workspace/pk-alert-manager/testt.py", line 23, in handle
async with connection.transaction():
File "/home/mario/workspace/pk-alert-manager/venv/lib/python3.10/site-packages/asyncpg/transaction.py", line 62, in __aenter__
await self.start()
File "/home/mario/workspace/pk-alert-manager/venv/lib/python3.10/site-packages/asyncpg/transaction.py", line 138, in start
await self._connection.execute(query)
File "/home/mario/workspace/pk-alert-manager/venv/lib/python3.10/site-packages/asyncpg/connection.py", line 318, in execute
return await self._protocol.query(query, timeout)
File "asyncpg/protocol/protocol.pyx", line 338, in query
RuntimeError: Task <Task pending name='Task-24' coro=<RequestHandler._handle_request() running at /home/mario/workspace/pk-alert-manager/venv/lib/python3.10/site-packages/aiohttp/web_protocol.py:435> cb=[Task.task_wakeup()]> got Future <Future pending cb=[Protocol._on_waiter_completed()]> attached to a different loop

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/mario/workspace/pk-alert-manager/venv/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
 resp = await request_handler(request)
 File "/home/mario/workspace/pk-alert-manager/venv/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle
 resp = await handler(request)
 File "/home/mario/workspace/pk-alert-manager/testt.py", line 21, in handle
async with pool.acquire() as connection:
 File "/home/mario/workspace/pk-alert-manager/venv/lib/python3.10/site-packages/asyncpg/pool.py", line 224, in release
raise ex
File "/home/mario/workspace/pk-alert-manager/venv/lib/python3.10/site-packages/asyncpg/pool.py", line 214, in release
await self._con.reset(timeout=budget)
File "/home/mario/workspace/pk-alert-manager/venv/lib/python3.10/site-packages/asyncpg/connection.py", line 1367, in reset
await self.execute(reset_query, timeout=timeout)
File "/home/mario/workspace/pk-alert-manager/venv/lib/python3.10/site-packages/asyncpg/connection.py", line 318, in execute
return await self._protocol.query(query, timeout)
File "asyncpg/protocol/protocol.pyx", line 323, in query
File "asyncpg/protocol/protocol.pyx", line 707, in asyncpg.protocol.protocol.BaseProtocol._check_state
asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress
topoleov commented 1 year ago

I found a solution:

it needs to pass loop as argument in run_app method

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