5 releases (3 breaking)

new 0.4.0 Dec 2, 2024
0.3.0 Dec 1, 2024
0.2.0 Jul 5, 2024
0.1.1 May 1, 2024
0.1.0 Mar 24, 2024

#204 in Game dev

Download history 305/week @ 2024-08-17 184/week @ 2024-08-24 278/week @ 2024-08-31 284/week @ 2024-09-07 225/week @ 2024-09-14 186/week @ 2024-09-21 111/week @ 2024-09-28 171/week @ 2024-10-05 154/week @ 2024-10-12 120/week @ 2024-10-19 86/week @ 2024-10-26 213/week @ 2024-11-02 105/week @ 2024-11-09 67/week @ 2024-11-16 154/week @ 2024-11-23 472/week @ 2024-11-30

826 downloads per month
Used in 2 crates

Apache-2.0 OR MIT

36KB
494 lines

Bevy Async Task

Discord MIT/Apache 2.0 Build status Dependency status crates.io docs.rs

A minimum crate for ergonomic abstractions to async programming in Bevy. There is full API support for wasm and native. Android and iOS are untested (Help needed).

Bevy Async Task provides Bevy system parameters to run asynchronous tasks in the background on web and native with timeouts and output capture.

Bevy version support

bevy bevy_async_task
0.15 0.3-0.4, main
0.14 0.2
0.13 0.1
<= 0.13 Unsupported

Usage

There are several examples for reference.

You can also run examples on web:

# Make sure the Rust toolchain supports the wasm32 target
rustup target add wasm32-unknown-unknown

cargo run_wasm --example simple

Polling in systems

Poll one task at a time with AsyncTaskRunner<T>:

async fn long_task() -> u32 {
    sleep(Duration::from_millis(1000)).await;
    5
}

fn my_system(mut task_runner: AsyncTaskRunner<u32>) {
    if task_runner.is_idle() {
        task_executor.start(long_task());
        info!("Started!");
    }

    match task_runner.poll() {
        Poll::Ready(v) => {
            info!("Received {v:?}");
        }
        Poll::Pending => {
            // Waiting...
        }
    }
}

Poll many similar tasks simultaneously with AsyncTaskPool<T>:

fn my_system(mut task_pool: AsyncTaskPool<u64>) {
    if task_pool.is_idle() {
        info!("Queueing 5 tasks...");
        for i in 1..=5 {
            task_pool.spawn(async move { // Closures work too!
                sleep(Duration::from_millis(i * 1000)).await;
                i
            });
        }
    }

    for status in task_pool.iter_poll() {
        if let Poll::Ready(v) = status {
            info!("Received {v:?}");
        }
    }
}

We also have a cross_system example.

Community

All Loopy projects and development happens in the Loopy Discord. The discord is open to the public.

Contributions are welcome by pull request. The Rust code of conduct applies.

License

Licensed under either of

at your option

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~43–79MB
~1.5M SLoC