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

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

Merged

Conversation

stankudrow
Copy link
Contributor

@stankudrow stankudrow commented Aug 26, 2024

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
Copy link
Contributor Author

@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
Copy link
Contributor Author

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

@stankudrow stankudrow closed this Aug 31, 2024
@stankudrow stankudrow reopened this Aug 31, 2024
@stankudrow
Copy link
Contributor Author

@pufit , thank you for your approval.

@long2ice long2ice merged commit 49764c5 into long2ice:dev Sep 5, 2024
2 checks passed
@long2ice
Copy link
Owner

long2ice commented Sep 5, 2024

Thanks!

stankudrow pushed a commit to stankudrow/asynch that referenced this pull request Sep 7, 2024
stankudrow pushed a commit to stankudrow/asynch that referenced this pull request Sep 21, 2024
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

Successfully merging this pull request may close these issues.

4 participants