Skip to content

Commit 95edd2e

Browse files
authored
async_compute example: don't block in the task (#13699)
# Objective - Fixes #13672 ## Solution - Don't use blocking sleep in the tasks, so that it won't block the task pool
1 parent 3d9b1e4 commit 95edd2e

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ serde = { version = "1", features = ["derive"] }
354354
bytemuck = "1.7"
355355
# Needed to poll Task examples
356356
futures-lite = "2.0.1"
357+
async-std = "1.12"
357358
crossbeam-channel = "0.5.0"
358359
argh = "0.1.12"
359360
thiserror = "1.0"

examples/async_tasks/async_compute.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bevy::{
88
tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task},
99
};
1010
use rand::Rng;
11-
use std::{thread, time::Duration};
11+
use std::time::Duration;
1212

1313
fn main() {
1414
App::new()
@@ -60,12 +60,10 @@ fn spawn_tasks(mut commands: Commands) {
6060
// spawn() can be used to poll for the result
6161
let entity = commands.spawn_empty().id();
6262
let task = thread_pool.spawn(async move {
63-
let mut rng = rand::thread_rng();
64-
65-
let duration = Duration::from_secs_f32(rng.gen_range(0.05..0.2));
63+
let duration = Duration::from_secs_f32(rand::thread_rng().gen_range(0.05..5.0));
6664

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

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

0 commit comments

Comments
 (0)