Skip to content

Unexpected attached to a different loop errors #402

Closed
@sk-

Description

@sk-

We had a hard to debug issue when testing async sqlalchemy.

When running the following test

from sqlalchemy.sql import text
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.pool import NullPool

engine = create_async_engine(
    "postgresql+asyncpg://user:password@localhost:5432/db_name", future=True#, poolclass=NullPool
)


async def test_echo():
    async with engine.begin() as conn:
        r = await conn.execute(text("SELECT 'hello' as echo"))
        assert r.scalar() == "hello"

async def test_echo2():
    async with engine.begin() as conn:
        r = await conn.execute(text("SELECT 'hello' as echo"))
        assert r.scalar() == "hello"

async def test_echo3():
    async with engine.begin() as conn:
        r = await conn.execute(text("SELECT 'hello' as echo"))
        assert r.scalar() == "hello"

we would get two errors:

attached to a different loop
<class 'asyncpg.exceptions._base.InterfaceError'>: cannot perform operation: another operation is in progress

This is fixed by having a global event loop as suggested in #38 (comment). Should there maybe be a command line option to enable this?

I'm opening this issue, as the creator of sqlalchemy suggested to report this to the underlying asyncio runner. See MagicStack/asyncpg#863 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions