Sqlachemy, asyncpg.exceptions._base.InterfaceError'>: cannot perform operation: another operation is in progress
- asyncpg version: 0.26.0
- PostgreSQL version:14.2-4
- Do you use a PostgreSQL SaaS? If so, which? Can you reproduce the issue with a local PostgreSQL install?:
- Python version:3.10.5
- Platform: Android Termux And Windows 10
- Do you use pgbouncer?:
- Did you install asyncpg with pip?:pip
- If you built asyncpg locally, which version of Cython did you use?:
- Can the issue be reproduced under both asyncio and uvloop?:
While adding data to db on first request it goes smoothly but after that
RuntimeError: Task <Task pending name='Task-6' coro=<AsyncToSync.main_wrap() running at /data/data/com.termux/files/usr/lib/python3.10/site-packages/asgiref/sync.py:284> cb=[_run_until_complete_cb() at /data/data/com.termux/files/usr/lib/python3.10/asyncio/base_events.py:184]> got Future <Future pending cb=[Protocol._on_waiter_completed()]> attached to a different loop
And on third request
raise translated_error from error sqlalchemy.exc.InterfaceError: (sqlalchemy.dialects.postgresql.asyncpg.InterfaceError) <class 'asyncpg.exceptions._base.InterfaceError'>: cannot perform operation: another operation is in progress
Here is my script
`
import asyncio, json
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.future import select
from sqlalchemy.orm import declarative_base, sessionmaker
from bson.json_util import dumps
from flask import Flask, request
from sqlalchemy.pool import QueuePool
from sqlalchemy import MetaData, Table, Column, String
engine = create_async_engine( "postgresql+asyncpg://yamato:yamato@localhost:5432/mydb", future=True, echo=True, pool_size=50, poolclass=QueuePool, )
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
Base = declarative_base()
class Keydb(Base): tablename = "keybase" id = Column(String, primary_key=True) json = Column(JSONB)
app = Flask(name)
async def init_db(): async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all)
@app.post("/api") async def add_data():
req = request.get_data(as_text=True)
js = json.loads(req)
if isinstance(js, dict):
async with async_session() as session:
async with session.begin():
await session.merge(Keydb(id=js["id"], json=js))
await session.commit()
# await sess.close()
return {"success": True}
return {"success": False}
asyncio.run(init_db()) app.run()
`
To reproduce error run this
curl "http://127.0.0.1:5000/api" -d '{"id":"ok","data":"no"}' curl "http://127.0.0.1:5000/api" -d '{"id":"ck","data":"yes"}'
Same error happens in windows and termux