wasm-mt-pool

Crates.iowasm-mt-pool
lib.rswasm-mt-pool
version0.1.2
sourcesrc
created_at2020-05-25 11:37:03.857532
updated_at2022-11-30 11:09:00.462193
descriptionA thread pool library based on wasm-mt
homepagehttps://github.com/w3reality/wasm-mt/tree/master/crates/pool
repositoryhttps://github.com/w3reality/wasm-mt/tree/master/crates/pool
max_upload_size
id245536
size29,689
(j-devel)

documentation

https://docs.rs/wasm-mt-pool

README

wasm-mt-pool

Docs | GitHub | Crate

crates MIT licensed CI

A thread pool library based on wasm-mt (github | crate).

Examples

You can run all the following apps in browser!

  • pool_exec - How to use wasm_mt_pool. [ live | source ]
  • http - A multithreaded server based on wasm_mt_pool. [ live | source ]
  • pool_arraybuffers - Demo of using ThreadPool::new_with_arraybuffers(). [ live | source ]

Getting started

Requirements:

Cargo.toml:

wasm-mt-pool = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_closure = "0.3"

Usage

#![feature(async_closure)]

use wasm_mt_pool::prelude::*;
use wasm_mt::utils::{console_ln, sleep};

let size = 2;
let pkg_js = "./pkg/pool_exec.js"; // path to `wasm-bindgen`'s JS binding
let pool = ThreadPool::new(size, pkg_js).and_init().await.unwrap();

let num = 4;

console_ln!("a) 💦 pool_exec! {} closures:", num);
for _ in 0..num {
    pool_exec!(pool, move || {
        console_ln!("a) closure: done.");
        Ok(JsValue::NULL)
    });
}

console_ln!("b) 💦 pool_exec! {} async closures:", num);
for _ in 0..num {
    pool_exec!(pool, async move || {
        sleep(1000).await;
        console_ln!("b) async closure: done.");
        Ok(JsValue::NULL)
    });
}

let cb = move |result| {
    console_ln!("callback: result: {:?}", result);
};

console_ln!("c) 💦 pool_exec! {} closures with callback:", num);
for _ in 0..num {
    pool_exec!(pool, move || {
        console_ln!("c) closure: done.");
        Ok(JsValue::from("C"))
    }, cb);
}

console_ln!("d) 💦 pool_exec! {} async closures with callback:", num);
for _ in 0..num {
    pool_exec!(pool, async move || {
        sleep(1000).await;
        console_ln!("d) async closure: done.");
        Ok(JsValue::from("D"))
    }, cb);
}

sleep(6_000).await; // Do sleep long enough to ensure all jobs are completed.
assert_eq!(pool.count_pending_jobs(), 0);
Commit count: 65

cargo fmt