3 releases
0.1.2 | Nov 30, 2022 |
---|---|
0.1.1 | Mar 15, 2021 |
0.1.0 | May 25, 2020 |
#826 in WebAssembly
25 downloads per month
Used in wasm-mt-pool-test
69KB
953 lines
wasm-mt-pool
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:
- rustc (nightly)
wasm-pack build
with the--target no-modules
option
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);
Dependencies
~8–12MB
~222K SLoC