neo4j / neo4j-python-driver

Neo4j Bolt driver for Python
https://neo4j.com/docs/api/python-driver/current/
Other
898 stars 186 forks source link

Update AsyncCondition to be based on Python 3.11 #879

Closed robsdedude closed 1 year ago

robsdedude commented 1 year ago

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 https://github.com/neo4j/neo4j-python-driver/issues/868

robsdedude commented 1 year ago

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.