Support async dbapi for SQLite
See original GitHub issueDescribe the bug Trying to use an AsyncEngine with SQLite raises an AwaitRequired exception. I don’t see anything specifically about async support in the SQLite dialect docs, but SQLite did work with async in 1.4.0b1.
Expected behavior
For create_async_engine("sqlite:///:memory:")
to provide a usable AsyncEngine
.
To Reproduce This code runs fine with 1.4.0b1 but fails with 1.4.0b2:
import asyncio
import sqlalchemy
from sqlalchemy.ext.asyncio import create_async_engine
metadata = sqlalchemy.MetaData()
vals = sqlalchemy.Table(
"vals",
metadata,
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
)
async def main():
engine = create_async_engine("sqlite:///:memory:")
async with engine.begin() as conn:
await conn.run_sync(metadata.create_all)
res = await conn.execute(vals.insert().values())
print(res.inserted_primary_key)
if __name__ == "__main__":
asyncio.run(main())
Error
Traceback (most recent call last):
File "/tmp/asynctest.py", line 23, in <module>
asyncio.run(main())
File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/tmp/asynctest.py", line 18, in main
res = await conn.execute(vals.insert().values())
File "/tmp/asynctest/lib/python3.9/site-packages/sqlalchemy/ext/asyncio/engine.py", line 404, in execute
result = await greenlet_spawn(
File "/tmp/asynctest/lib/python3.9/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 121, in greenlet_spawn
raise exc.AwaitRequired(
sqlalchemy.exc.AwaitRequired: The current operation required an async execution but none was detected. This will usually happen when using a non compatible DBAPI driver. Please ensure that an async DBAPI is used. (Background on this error at: http://sqlalche.me/e/14/xd1r)
Versions.
- OS: Linux, Debian Bullseye
- Python: 3.9.1
- SQLAlchemy: 1.4.0b2
- Database: SQLite
- DBAPI: pysqlite
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (10 by maintainers)
Top Results From Across the Web
Using asyncio extension with SQLite backend broken by ...
InvalidRequestError: The asyncio extension requires an async driver to be used. The loaded 'pysqlite' is not async. Is this due to an ...
Read more >Async SQL (Relational) Databases - FastAPI
It is compatible with: PostgreSQL; MySQL; SQLite. In this example, we'll use SQLite, because it uses a single file and Python has integrated...
Read more >Asynchronous I/O (asyncio) - SQLAlchemy 1.4 Documentation
When working within an event handler that is within an asyncio context, objects like the Connection continue to work in their usual “synchronous ......
Read more >An Asynchronous I/O Module For SQLite
The asynchronous IO extension consists of a single file of C code (sqlite3async.c), and a header file (sqlite3async.h), located in the ext/async ......
Read more >FastAPI tutorial with aiosqlite and real async calls
Further to a conversation in the dev meeting last Thursday, I wanted to see if I could get the FastAPI tutorial going with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The wip in gerrit more or less works, but has some problems in the tests that I was not able to solve. IIRC it seemed to leak connections what are not closed leading to the interpreter not closing, but I’m not sure if it’s the sqlalchemy test suite fault and aiosqlite is simpy very sensitive to it, the integration code or aiosqlit. I’ll try to look again into it
if aiosqlite is using a thread I’m a little non-optimistic it can handle a lot of requests but let’s see!