Skip to content

Commit

Permalink
async_compute example: don't block in the task (#13699)
Browse files Browse the repository at this point in the history
# Objective

- Fixes #13672 

## Solution

- Don't use blocking sleep in the tasks, so that it won't block the task
pool
  • Loading branch information
mockersf authored Jun 6, 2024
1 parent 3d9b1e4 commit 95edd2e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ serde = { version = "1", features = ["derive"] }
bytemuck = "1.7"
# Needed to poll Task examples
futures-lite = "2.0.1"
async-std = "1.12"
crossbeam-channel = "0.5.0"
argh = "0.1.12"
thiserror = "1.0"
Expand Down
8 changes: 3 additions & 5 deletions examples/async_tasks/async_compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::{
tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task},
};
use rand::Rng;
use std::{thread, time::Duration};
use std::time::Duration;

fn main() {
App::new()
Expand Down Expand Up @@ -60,12 +60,10 @@ fn spawn_tasks(mut commands: Commands) {
// spawn() can be used to poll for the result
let entity = commands.spawn_empty().id();
let task = thread_pool.spawn(async move {
let mut rng = rand::thread_rng();

let duration = Duration::from_secs_f32(rng.gen_range(0.05..0.2));
let duration = Duration::from_secs_f32(rand::thread_rng().gen_range(0.05..5.0));

// Pretend this is a time-intensive function. :)
thread::sleep(duration);
async_std::task::sleep(duration).await;

// Such hard work, all done!
let transform = Transform::from_xyz(x as f32, y as f32, z as f32);
Expand Down

0 comments on commit 95edd2e

Please sign in to comment.