Crates.io | wasm-mt-pool |
lib.rs | wasm-mt-pool |
version | 0.1.2 |
source | src |
created_at | 2020-05-25 11:37:03.857532 |
updated_at | 2022-11-30 11:09:00.462193 |
description | A thread pool library based on wasm-mt |
homepage | https://github.com/w3reality/wasm-mt/tree/master/crates/pool |
repository | https://github.com/w3reality/wasm-mt/tree/master/crates/pool |
max_upload_size | |
id | 245536 |
size | 29,689 |
A thread pool library based on wasm-mt
(github | crate).
You can run all the following apps in browser!
wasm_mt_pool
. [ live | source ]wasm_mt_pool
. [ live | source ]ThreadPool::new_with_arraybuffers()
. [ live | source ]Requirements:
wasm-pack build
with the --target no-modules
optionCargo.toml:
wasm-mt-pool = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_closure = "0.3"
#![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);