Add option to use asyncio for AsyncWrapper#38262
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces an optional asyncio-based execution mode for the AsyncWrapper in Apache Beam's Python SDK. By allowing the use of coroutines instead of relying solely on ThreadPoolExecutor, this change aims to enhance the efficiency and parallelism of remote RPC calls within asynchronous DoFns. The implementation includes robust management of a background event loop and comprehensive updates to the test suite to validate the new functionality. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
Assigning reviewers: R: @tvalentyn for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
|
R: @damccorm |
|
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment |
damccorm
left a comment
There was a problem hiding this comment.
Thanks! Generally the approach LGTM
| process_result = self._sync_fn.process(element, *args, **kwargs) | ||
| bundle_result = self._sync_fn.finish_bundle() | ||
|
|
||
| if not process_result: |
There was a problem hiding this comment.
Optional nit: thoughts on wrapping this in a small local function so that we just need to call something like:
process_result = gather_async_results(process_result)
bundle_result = gather_async_results(bundle_result)
| id_fn: A function that returns a hashable object from an element. This | ||
| will be used to track items instead of the element's default hash. | ||
| use_asyncio: If true, use asyncio and coroutines to process items. If | ||
| false, use ThreadPoolExecutor. |
There was a problem hiding this comment.
In our tests, has asyncio generally performed better? Are there reasons to default it off?
There was a problem hiding this comment.
Regardless, this doc should have info on why a user would choose one vs the other (or if its almost always one then we should just make this a kwarg)
There was a problem hiding this comment.
I think mostly in cases of where we want to run CPU intensive work on threads the default will be better since we only have 1 main event loop in async mode. But yeah definitely would need to add some more info. Done.
|
|
||
|
|
||
| class AsyncTest(unittest.TestCase): | ||
| class _AsyncTestBase: |
There was a problem hiding this comment.
https://stackoverflow.com/questions/38729007/parametrize-class-tests-with-pytest is probably the more pythonic way to do this
There was a problem hiding this comment.
Done. Turns out to use parameterized class instead of the pytest one since it seems like other test files in beam uses the parameterized one.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces an asyncio-based execution mode for AsyncWrapper, allowing elements to be processed via coroutines in addition to the existing ThreadPoolExecutor. The changes include the management of a background event loop thread and the implementation of async-specific processing logic. Feedback highlights critical bugs in the async_fn_process method, which currently fails to await coroutines and risks blocking the event loop. Additionally, a thread-safety issue was identified in reset_state, where shared class variables are modified without proper locking.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Danny McCormick <dannymccormick@google.com>
Add option to use asyncio for AsyncWrapper to increase parallelism remote RPC calls.
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.