Skip to content

Conversation

@syszery
Copy link
Contributor

@syszery syszery commented Oct 24, 2025

Objective

In #21598 and the related Discord discussion, it was pointed out that the async_compute example promotes an inefficient and potentially error-prone pattern: future::block_on(poll_once).

It is now recommended to use bevy::tasks::futures::check_ready instead, which is much cheaper and avoids blocking the main thread while waiting on the future. Another valid approach is to pass a channel into the async tasks and detach them, letting the channel handle task readiness.

This PR implements both suggestions.

Solution

  • Updated async_compute example
    Replaced the use of future::block_on(poll_once) with the recommended check_ready.
    The change is minimal and limited to the directly affected lines.

  • Added new example: async_channel_pattern
    Demonstrates how to spawn async tasks using a channel-based communication pattern.

    • Each task is executed on a separate thread via AsyncComputeTaskPool.
    • Results (cube positions) are sent back through a CubeChannel once completed.
    • Tasks are detached to run fully asynchronously, ensuring the main thread remains unblocked.
    • A rotating light in the scene visually indicates that the frame loop remains responsive.

I am relatively new to both Bevy and async Rust, and I genuinely appreciate any feedback or suggestions to improve these examples — especially regarding idiomatic async patterns and best practices for Bevy task management.

Fixes #21598

@syszery syszery marked this pull request as ready for review October 24, 2025 12:05
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Async Deals with asynchronous abstractions labels Oct 27, 2025
@alice-i-cecile alice-i-cecile added the C-Examples An addition or correction to our examples label Oct 27, 2025
@MalekiRe
Copy link
Contributor

Both of these examples look good to me, using check_ready instead of block_on is just correct. The channel example looks good, I would actually like to see an example of bi-directional communication from ecs <-> the async task, but that's just in general something that I think would be good to add as another more complex example of how you can use channels ( I find people get confused about the idea that you need to have senders and receivers on both ends in order to do bi-directional communication ).

I think in no way that should block this from being merged, just something else you could PR!

implement review feedback

Co-authored-by: François Mockers <francois.mockers@vleue.com>
@github-actions
Copy link
Contributor

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-21647

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

@syszery
Copy link
Contributor Author

syszery commented Oct 28, 2025

Both of these examples look good to me, using check_ready instead of block_on is just correct. The channel example looks good, I would actually like to see an example of bi-directional communication from ecs <-> the async task, but that's just in general something that I think would be good to add as another more complex example of how you can use channels ( I find people get confused about the idea that you need to have senders and receivers on both ends in order to do bi-directional communication ).

I think in no way that should block this from being merged, just something else you could PR!

Thanks @MalekiRe for your feedback and the suggestion! That’s a really good idea. I’ll take a look into possible ways to create a meaningful bi-directional example.

@mockersf mockersf added this pull request to the merge queue Oct 28, 2025
Merged via the queue into bevyengine:main with commit 1bd6f42 Oct 28, 2025
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS Entities, components, systems, and events C-Examples An addition or correction to our examples D-Async Deals with asynchronous abstractions S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Async compute task example uses bad practices.

4 participants