Skip to content

Update AsyncCondition to be based on Python 3.11 #879

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

Merged
merged 4 commits into from
Dec 23, 2022

Conversation

robsdedude
Copy link
Member

@robsdedude robsdedude commented Dec 16, 2022

Most notably, this changes how the async driver behaves when created while no
event loop is running. Previously, this would lead to hard to predict errors
only occurring once the connection pool ran full.

Now, the async synchronization primitives will defer binding to an event loop
until they're used in an async context for the first time. This simply allows
users to safely create a driver without a running event loop (in a sync
context) and later use it in an async context.

Important note: this will likely only work when the user is on Python 3.10+
because the driver also relies on synchronization primitives that come with
asyncio. So their behavior depends on the used Python version.

This PR got sparked from and
closes #868

Most notably, this changes how the async driver behaves when created while no
event loop is running. Previously, this would lead to hard to predict errors
only occurring once the connection pool ran full.

The creation of the async driver will fail directly when created without a
running async loop. This mirrors the behavior of `asyncio.Condition` that was
changed in the same way from Python 3.9 to 3.11.

While this might be considered a breaking change, having a partially working
driver that crashes once under real load poses the real risk of not catching the
mistake until the application is in production, which is much worse than failing
as early as possible.
@robsdedude
Copy link
Member Author

In fact, I misread the new Condition code in Python 3.10+. It explicitly fixes the issue that used to arises from creating async synchronization primitives when not event loop was running. The condition now binds to a loop the first time it's actually used in an async context. This is even better as it will just allow users to create an async driver in a sync function and use it asynchronously later.

I'll update the PR description.

Copy link
Contributor

@bigmontz bigmontz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒

@robsdedude robsdedude merged commit 496480e into neo4j:5.0 Dec 23, 2022
@robsdedude robsdedude changed the title Update AyncCondition to be based on Python 3.11 Update AsyncCondition to be based on Python 3.11 Jan 17, 2023
@robsdedude robsdedude deleted the update-async-condition branch March 30, 2023 07:14
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.

Async: The future belongs to a different loop than the one specified as the loop argument
2 participants