Skip to content

Add option to use asyncio for AsyncWrapper#38262

Merged
damccorm merged 7 commits intoapache:masterfrom
AMOOOMA:remote
May 4, 2026
Merged

Add option to use asyncio for AsyncWrapper#38262
damccorm merged 7 commits intoapache:masterfrom
AMOOOMA:remote

Conversation

@AMOOOMA
Copy link
Copy Markdown
Contributor

@AMOOOMA AMOOOMA commented Apr 21, 2026

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:

  • Mention the appropriate issue in your description (for example: 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, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

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)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Asyncio Support: Added an option to use asyncio for AsyncWrapper to improve parallelism for remote RPC calls.
  • Background Event Loop: Implemented a background thread to manage the asyncio event loop, ensuring it can be used alongside existing thread-based processing.
  • Test Coverage: Updated existing tests to support both ThreadPoolExecutor and asyncio modes, ensuring parity and stability across both execution strategies.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @tvalentyn for label python.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@AMOOOMA
Copy link
Copy Markdown
Contributor Author

AMOOOMA commented Apr 28, 2026

R: @damccorm

@github-actions
Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

Copy link
Copy Markdown
Contributor

@damccorm damccorm left a comment

Choose a reason for hiding this comment

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

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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread sdks/python/apache_beam/transforms/async_dofn.py Outdated
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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In our tests, has asyncio generally performed better? Are there reasons to default it off?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@damccorm
Copy link
Copy Markdown
Contributor

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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.

Comment thread sdks/python/apache_beam/transforms/async_dofn.py Outdated
Comment thread sdks/python/apache_beam/transforms/async_dofn.py Outdated
AMOOOMA and others added 5 commits April 28, 2026 14:05
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Danny McCormick <dannymccormick@google.com>
Copy link
Copy Markdown
Contributor

@damccorm damccorm left a comment

Choose a reason for hiding this comment

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

Thanks!

@damccorm damccorm merged commit 637231c into apache:master May 4, 2026
144 of 156 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants