A thread pool library based on wasm-mt
(github | crate).
You can run all the following apps in browser!
wasmmtpool
. [ [live](https://w3reality.github.io/wasm-mt/crates/pool/examples/poolexec/index.html) | [source](https://github.com/w3reality/wasm-mt/tree/master/crates/pool/examples/pool_exec) ]wasmmtpool
. [ [live](https://w3reality.github.io/wasm-mt/crates/pool/examples/http/index.html) | [source](https://github.com/w3reality/wasm-mt/tree/master/crates/pool/examples/http) ]ThreadPool::newwitharraybuffers()
. [ [live](https://w3reality.github.io/wasm-mt/crates/pool/examples/poolarraybuffers/index.html) | [source](https://github.com/w3reality/wasm-mt/tree/master/crates/pool/examples/pool_arraybuffers) ]Requirements:
wasm-pack build
with the --target no-modules
optionCargo.toml:
toml
wasm-mt-pool = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_closure = "0.2"
```rust
use wasmmtpool::prelude::*; use wasmmt::utils::{consoleln, sleep};
let size = 2;
let pkgjs = "./pkg/poolexec.js"; // path to wasm-bindgen
's JS binding
let pool = ThreadPool::new(size, pkgjs).andinit().await.unwrap();
let num = 4;
consoleln!("a) 💦 poolexec! {} closures:", num); for _ in 0..num { poolexec!(pool, move || { consoleln!("a) closure: done."); Ok(JsValue::NULL) }); }
consoleln!("b) 💦 poolexec! {} async closures:", num); for _ in 0..num { poolexec!(pool, async move || { sleep(1000).await; consoleln!("b) async closure: done."); Ok(JsValue::NULL) }); }
let cb = move |result| { console_ln!("callback: result: {:?}", result); };
consoleln!("c) 💦 poolexec! {} closures with callback:", num); for _ in 0..num { poolexec!(pool, move || { consoleln!("c) closure: done."); Ok(JsValue::from("C")) }, cb); }
consoleln!("d) 💦 poolexec! {} async closures with callback:", num); for _ in 0..num { poolexec!(pool, async move || { sleep(1000).await; consoleln!("d) async closure: done."); Ok(JsValue::from("D")) }, cb); }
sleep(6000).await; // Do sleep long enough to ensure all jobs are completed. asserteq!(pool.countpendingjobs(), 0); ```