long2ice / asynch

An asyncio ClickHouse Python Driver with native (TCP) interface support.
https://github.com/long2ice/asynch
Apache License 2.0
185 stars 43 forks source link

Fix the `async for row in cursor:` infinite loop error #112

Closed stankudrow closed 2 months ago

stankudrow commented 2 months ago

When giving an answer in the discussion #100 about using stream results, I designed the following example:

import asyncio

from asynch import Connection, DictCursor

async def main() -> None:
    for stmt in ("SELECT 21", "SELECT 42 WHERE 1 != 1"):
        print(f"Executing the statement {stmt!r}.")

        async with Connection() as conn:
            async with conn.cursor(cursor=DictCursor) as cursor:
                cursor.set_stream_results(stream_results=True, max_row_buffer=1000)
                await cursor.execute(stmt)
                for row in await cursor.fetchall():
                    print(row)

            print("Intermezzo")

            async with conn.cursor(cursor=DictCursor) as cursor:
                cursor.set_stream_results(stream_results=True, max_row_buffer=1000)
                await cursor.execute(stmt)
                async for row in cursor:
                    print(row)
        print(f"The statement {stmt!r} was tested.\n")

if __name__ == "__main__":
    asyncio.run(main())

The async for part gets stuck in the infinite loop because fetchone in the __anext__() method returns an empty result which ic not None to stop the async iteration.

Partial API-non-breaking changes, mostly minor grooming and refactoring details, moved here from the PR #111 .

stankudrow commented 2 months ago

@long2ice , @KuzenkovAG, @DFilyushin, @gnomeby , @DaniilAnichin , please help me with reviewing and improving this PR. Is this enough or other places in the code must be revised as well?

stankudrow commented 2 months ago

@long2ice , shall we review this PR? Would you not mind to give an answer to this question #100 ?

stankudrow commented 2 months ago

@pufit , thank you for your approval.

long2ice commented 2 months ago

Thanks!