You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
importasyncioimportdatabasesimportsqlalchemyassa# import logging# logging.basicConfig(level=logging.DEBUG)DB_URL="sqlite:////tmp/test.db"metadata=sa.MetaData()
table=sa.Table(
"t1", metadata,
sa.Column("id", sa.Integer()),
sa.Column("data", sa.String()),
)
database=databases.Database(DB_URL)
asyncdefinit_db():
engine=sa.create_engine(DB_URL)
metadata.create_all(engine)
awaitdatabase.connect()
asyncdefwork(start: int, n=5, data="a"):
asyncwithdatabase.transaction():
query=table.insert()
foriinrange(start, n):
awaitdatabase.execute(query, {"id": i, "data": data})
print(f"Ok {start}")
asyncdefmain_async():
awaitinit_db()
futures= []
some_query=table.select()
# Error is raised if there is query executed before creating futures# Doesn't matter if transaction is explicit or notasyncwithdatabase.transaction():
# !!! If I remove this query (from parent task), it works fineawaitdatabase.fetch_all(some_query)
step=5foriinrange(0, 4*step, step):
fut=asyncio.ensure_future(work(i, i+step))
futures.append(fut)
awaitasyncio.wait(futures)
print("Done")
loop=asyncio.get_event_loop()
loop.run_until_complete(main_async())
Error with sqlite database:
databases/core.py", line 305, in commit
assert self._connection._transaction_stack[-1] is self
Error with postgresql database:
File "asyncpg/protocol/protocol.pyx", line 301, in query
File "asyncpg/protocol/protocol.pyx", line 659, in asyncpg.protocol.protocol.BaseProtocol._check_state
asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress