#web-worker #async-task #task-execution #web-apps #future #tokio-task

async_wasm_task

Manage the concurrency of async tasks in webassembly Rust

5 releases

0.2.3 Oct 8, 2023
0.2.2 Oct 7, 2023
0.2.1 Oct 7, 2023
0.2.0 Oct 7, 2023
0.1.0 Oct 6, 2023

#472 in WebAssembly

21 downloads per month

MIT license

28KB
328 lines

async_wasm_task

Crates.io Documentation License

bandicam 2023-10-07 17-34-36-577

Tested with Rust-In-Flutter

async_wasm_task is a Rust library that provides an API for managing asynchronous tasks and Rust Futures in a JavaScript environment, closely resembling the familiar patterns of tokio::task. It is designed to allow async Rust code to work seamlessly with JavaScript in web applications, leveraging web workers for parallel task execution.

Focusing on wasm32-unknown-unknown target, this library assumes that you're compilng your Rust project with wasm-pack. It spawns parallel web workers(threads) with the the same JavaScript file that the original web worker has been created with, so you might not need to deal with JavaScript code yourself.

The number of web workers are automatically adjusted adapting to the parallel tasks that has been queued by spawn_blocking. Refer to the docs for additional details.

Features

  • Familiar API: If you're familiar with tokio::task, you'll feel right at home with async_wasm_task. It provides similar functionality and follows the same patterns for spawning and managing asynchronous tasks.

  • Web Worker Integration: async_wasm_task adapts to the JavaScript environment by utilizing web workers under the hood. This means you can write Rust code that runs concurrently and efficiently in web applications.

  • Spawn Async and Blocking Tasks: You can spawn both asynchronous and blocking tasks. Asynchronous tasks allow you to perform non-blocking operations, while blocking tasks are suitable for compute-heavy or synchronous tasks.

Usage

Add this library to your Cargo.toml:

[dependencies]
async_wasm_task = "[latest-version]"

Here's a simple example of how to use async_wasm_task:

use async_wasm_task::{spawn, spawn_blocking, yield_now};

async fn start() {
    let async_join_handle = spawn(async {
        // Your asynchronous code here.
        // This will run concurrently
        // in the same web worker(thread).
    });
    let blocking_join_handle = spawn_blocking(|| {
        // Your blocking code here.
        // This will run parallelly
        // in the external pool of web workers.
    });
    let async_result = async_join_handle.await;
    let blocking_result = blocking_join_handle.await;
    for i in 1..1000 {
        // Some repeating task here
        // that shouldn't block the JavaScript runtime.
        yield_now().await;
    }
}

Documentation

Detailed documentation can be found on docs.rs.

Contributing

Contributions are welcome! If you have any suggestions, bug reports, or want to contribute to the development of async_wasm_task, please open an issue or submit a pull request.

Dependencies

~7.5–10MB
~183K SLoC