Skip to content
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

Support for asyncio in sqlalchemy-iris #13

Open
intermask opened this issue Apr 17, 2024 · 6 comments
Open

Support for asyncio in sqlalchemy-iris #13

intermask opened this issue Apr 17, 2024 · 6 comments

Comments

@intermask
Copy link

sqlalchemy supports asyncio (Asynchronous I/O (asyncio) — SQLAlchemy 2.0 Documentation).

Can this support be added to sqlalchemy-iris.

@daimor
Copy link
Member

daimor commented Apr 17, 2024

Did you try to use it?

@intermask
Copy link
Author

I tried running following sample code to test asyncio from Jupyter Notebook, get the error "InvalidRequestError: The asyncio extension requires an async driver to be used. The loaded 'iris' is not async."

import asyncio
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker

Define an async function to test asyncio with SQLAlchemy

async def test_asyncio_sqlalchemy():
# Create an async engine
DATABASE_URL = "iris://_SYSTEM:SYS@localhost:1972/USER" # IRIS URL
async_engine = create_async_engine(DATABASE_URL, echo=True)

# Create a table metadata
metadata = MetaData()

# Define a sample table
sample_table = Table(
    'sample_table', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(50)),
)

# Create the table
async with async_engine.begin() as conn:
    await conn.run_sync(metadata.create_all)

# Create an AsyncSession
async_session = sessionmaker(
    async_engine, expire_on_commit=False, class_=AsyncSession
)

# Add data to the table using the AsyncSession
async with async_session() as session:
    async with session.begin():
        session.add_all([
            sample_table.insert().values(name="Asyncio User 1"),
            sample_table.insert().values(name="Asyncio User 2")
        ])
    await session.commit()

# Query data from the table using the AsyncSession
async with async_session() as session:
    result = await session.execute(sample_table.select())
    for row in result.scalars().all():
        print(row)

Run the async function

#asyncio.run(test_asyncio_sqlalchemy())
await test_asyncio_sqlalchemy()

@daimor
Copy link
Member

daimor commented Apr 18, 2024

I'll do some changes for it

@daimor
Copy link
Member

daimor commented Apr 18, 2024

try installing version 0.13.4b2
and update uri from iris:// to iris+irisasync://

@intermask
Copy link
Author

Thanks Dmitry, now I am getting error on the next line: async_engine = create_async_engine(DATABASE_URL, echo=True)

The error is: ArgumentError: Pool class QueuePool cannot be used with asyncio engine (Background on this error at: https://sqlalche.me/e/20/pcls)

@daimor
Copy link
Member

daimor commented Apr 20, 2024

I dug it a bit, and it will need much more work to bring async support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants