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

feat(python): More robust handling of async database calls #15202

Merged

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Mar 21, 2024

Ref: #15162 (comment)

@stinodego: This brings forward additional async industrialisation that I was working on yesterday, while adding some initial SurrealDB1 integration (have got a new SurrealDBCursorProxy class working in another branch2).

I haven't been able to reproduce the ResourceWarning locally yet, but this may solve it by explicitly closing any event loops that we know we created in-context (and it doesn't seem to have occurred in these tests, so... maybe? 🤔)

Also improves/fixes interaction with Notebooks/Jupyter (nesting asyncio calls).

Footnotes

  1. SurrealDB features: https://surrealdb.com/features

  2. SurrealDB + Polars: https://github.com/surrealdb/surrealdb.py/pull/91#issuecomment-2009802667

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Mar 21, 2024
Copy link

codecov bot commented Mar 21, 2024

Codecov Report

Attention: Patch coverage is 41.17647% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 81.24%. Comparing base (103bb66) to head (9bd5f25).
Report is 23 commits behind head on main.

❗ Current head 9bd5f25 differs from pull request most recent head 638642f. Consider uploading reports for the commit 638642f to get more accurate results

Files Patch % Lines
py-polars/polars/io/database/_executor.py 41.17% 9 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15202      +/-   ##
==========================================
- Coverage   81.25%   81.24%   -0.01%     
==========================================
  Files        1354     1354              
  Lines      175390   175406      +16     
  Branches     2518     2520       +2     
==========================================
+ Hits       142505   142506       +1     
- Misses      32404    32417      +13     
- Partials      481      483       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

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

Could you add tests for the cases this is trying to solve? And add nest_asyncio to the requirements-dev.txt.

@alexander-beedie
Copy link
Collaborator Author

alexander-beedie commented Mar 21, 2024

Could you add tests for the cases this is trying to solve?

Will see what I can come up with; shouldn't be too hard to setup an outer asyncio loop context and test within that to trigger nesting awareness, hmm 🤔

And add nest_asyncio to the requirements-dev.txt.

Done (had it in the other branch, but missed copying it over 😅)

@alexander-beedie
Copy link
Collaborator Author

alexander-beedie commented Mar 21, 2024

Could you add tests for the cases this is trying to solve?

Done; created/committed a test that simulates the event loop conditions found inside Notebooks and other nested async scenarios (such as interop with SurrealDB in an async context manager, which will follow later) 👌

(If you comment out the nest_asyncio.apply() in "_executor.py" you'll see that the test fails with a RuntimeError "asyncio.run() cannot be called from a running event loop", confirming the utility of the new code).

Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

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

Thanks Alex, I think we can go ahead with this 👍

I'm only thinking that maybe we can add a warning to the read_database docstring that using async engines is currently considered unstable. That way at least we have some leeway if we choose to go for a different solution in the near future. I'll let you be the judge.

@alexander-beedie
Copy link
Collaborator Author

I'm only thinking that maybe we can add a warning to the read_database docstring that using async engines is current considered unstable. // I'll let you be the judge.

Sounds like a good idea to me; I'll label the support as being considered experimental for now in my imminent follow-up (coming later tonight or first thing tomorrow) that adds SurrealDB support 😎👍

@alexander-beedie alexander-beedie merged commit bc91c62 into pola-rs:main Mar 23, 2024
12 checks passed
@alexander-beedie alexander-beedie deleted the improve-async-database-calls branch March 23, 2024 12:56
@alexander-beedie alexander-beedie added the A-io-database Area: reading/writing to databases label Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io-database Area: reading/writing to databases enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants