-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature Proposal]: async support #1031
Comments
currenty testing with this code: from typing import Type
from duckdb_engine import Dialect
from sqlalchemy import URL, pool
from sqlalchemy.dialects import registry # noqa
class AsyncDuckDB(Dialect):
driver = 'aioduckdb'
supports_statement_cache = True
is_async = True
@classmethod
def get_pool_class(cls, url: URL) -> Type[pool.Pool]:
if url.database == ':memory:':
return pool.NullPool
else:
return pool.StaticPool
registry.register('duckdb.aioduckdb', 'engine.aioduckdb', 'AsyncDuckDB') it is safe to set supports_statement_cache to True with this driver? here the example: #!/usr/bin/env python3
import asyncio
from sqlalchemy import Column, Integer, Sequence, String, Select
from sqlalchemy.ext.asyncio import AsyncAttrs
from sqlalchemy.ext.asyncio import async_sessionmaker
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import DeclarativeBase
from engine import aioduckdb # noqa
class Base(AsyncAttrs, DeclarativeBase):
pass
class FakeModel(Base):
__tablename__ = 'fake'
id = Column(Integer, Sequence('fakemodel_id_sequence'), primary_key=True)
name = Column(String)
def __str__(self):
return f'FakeModel(id={self.id}, name={self.name})'
async def main():
engine = create_async_engine('duckdb+aioduckdb:///:memory:', echo=True)
# engine = create_async_engine('duckdb+aioduckdb:///test.db', echo=True)
async_session = async_sessionmaker(engine, expire_on_commit=False)
async with async_session() as session:
async with session.begin():
conn = await session.connection()
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
session.add(FakeModel(name='Frank'))
session.add(FakeModel(name='Joe'))
for result in await session.scalars(Select(FakeModel)):
print(result)
for result in await session.scalars(Select(FakeModel)):
print(result)
result = await session.stream(Select(FakeModel))
async for e in result.scalars():
print(e)
await session.commit()
if __name__ == '__main__':
asyncio.run(main()) output:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
According to https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html,
It For a SQL DB implementation, it require relevant asyncio driver to make SQLAlchemy support this database in asyncio mode.
It would be awesome to add asyncio feature in
duckdb_engine
.The text was updated successfully, but these errors were encountered: