Skip to content

Stream async iterator very slow #286

@comodore31

Description

@comodore31

Hello,

Parsing the iterator returned by the async function stream()
https://www.psycopg.org/psycopg3/docs/api/cursors.html?highlight=stream#psycopg.AsyncCursor.stream
is 100 times slower than parsing the returning of fetchall()

eg:

from datetime import datetime
import sys
import asyncio
import psycopg

async def main():

    async with await psycopg.AsyncConnection.connect(conninfo='postgresql://xxxxxxxxx', connect_timeout=7200, autocommit=True) as con:
        async with con.cursor() as cur:

            t = datetime.now()
            i = 0
            for row in (await (await cur.execute("select 'test' a, t b from generate_series(1, 100000) t")).fetchall()):
                i += 1
            print(datetime.now() - t)
            # 0.08s

            t = datetime.now()
            i = 0
            async for row in cur.stream("select 'test' a, t b from generate_series(1, 100000) t"):
                i += 1
            print(datetime.now() - t)
            # 8.35s !!!!!!! x100 slower

if __name__ == '__main__':

    if sys.platform == "win32":
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

    asyncio.run(main())

Results:

0:00:00.088759
0:00:08.105972

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