{"id":15726018,"url":"https://github.com/wngr/wasm-futures-executor","last_synced_at":"2025-07-11T17:47:17.264Z","repository":{"id":39861558,"uuid":"399592877","full_name":"wngr/wasm-futures-executor","owner":"wngr","description":"Executor for asynchronous task based on wasm web workers.","archived":false,"fork":false,"pushed_at":"2024-10-03T16:46:29.000Z","size":144,"stargazers_count":33,"open_issues_count":3,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-26T16:56:14.947Z","etag":null,"topics":["asynchronous-programming","concurrency","rust","wasm","web"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wngr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE2.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-24T20:12:17.000Z","updated_at":"2024-10-03T16:46:34.000Z","dependencies_parsed_at":"2024-12-27T12:01:29.786Z","dependency_job_id":null,"html_url":"https://github.com/wngr/wasm-futures-executor","commit_stats":{"total_commits":33,"total_committers":3,"mean_commits":11.0,"dds":0.09090909090909094,"last_synced_commit":"c7a8b3eae174057721161c53b3bd984fc243b35e"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wngr%2Fwasm-futures-executor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wngr%2Fwasm-futures-executor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wngr%2Fwasm-futures-executor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wngr%2Fwasm-futures-executor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wngr","download_url":"https://codeload.github.com/wngr/wasm-futures-executor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246758370,"owners_count":20828919,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asynchronous-programming","concurrency","rust","wasm","web"],"created_at":"2024-10-03T22:25:24.198Z","updated_at":"2025-04-02T05:09:54.005Z","avatar_url":"https://github.com/wngr.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# wasm-futures-executor\n\n[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](https://github.com/wngr/wasm-futures-executor)\n[![Cargo](https://img.shields.io/crates/v/wasm-futures-executor.svg)](https://crates.io/crates/wasm-futures-executor)\n[![Documentation](https://docs.rs/wasm-futures-executor/badge.svg)](https://docs.rs/wasm-futures-executor)\n\nThis crate provides an executor for asynchronous task with the same\nAPI as [`futures_executor::ThreadPool`] targeting the web browser\nenvironment. Instead of using spawning threads via `std::thread`, web\nworkers are created. This crate tries hard to make this process as\nseamless and painless as possible.\n\n[`futures_executor::ThreadPool`]: https://docs.rs/futures-executor/0.3.16/futures_executor/struct.ThreadPool.html\n\n## Sample Usage\n```rust\nuse futures::channel::mpsc;\nuse futures::StreamExt;\nuse js_sys::Promise;\nuse wasm_bindgen::prelude::*;\nuse wasm_futures_executor::ThreadPool;\n\n#[wasm_bindgen]\npub async fn start() -\u003e Result\u003cJsValue, JsValue\u003e {\n    let pool = ThreadPool::max_threads().await?;\n    let (tx, mut rx) = mpsc::channel(10);\n    for i in 0..20 {\n        let mut tx_c = tx.clone();\n        pool.spawn_ok(async move {\n            tx_c.start_send(i * i).unwrap();\n        });\n    }\n    drop(tx);\n    let mut i = 0;\n    while let Some(x) = rx.next().await {\n        i += x;\n    }\n    Ok(i.into())\n}\n```\n.. and using it:\n```javascript\nimport init, { start } from './sample.js';\n\nasync function run() {\n  await init();\n\n  const res = await start();\n  console.log(\"result\", res);\n}\nrun();\n```\n\nAnd build your project with:\n```sh\nRUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' \\\n  cargo +nightly build --target wasm32-unknown-unknown --release -Z build-std=std,panic_abort\n\nwasm-bindgen \\\n  ./target/wasm32-unknown-unknown/release/sample.wasm \\\n  --out-dir . \\\n  --target web \\\n  --weak-refs\n``` \n.. or if you want to use `wasm-pack build -t web`, make sure to set up\nthe nightly toolchain and the proper `RUSTFLAGS` (for example by\nproviding creating the `rust-toolchain.toml` and `.cargo/config` files\nlike done in this repo).\n\nPlease have a look at the [sample](./sample) for a complete end-to-end\nexample project without bundlers, and\n[sample-webpack](./sample-webpack) using Webpack 5.\n\nNote: This crate requires usage of the `web` target of\n`wasm-bindgen`/`wasm-pack`. Given the broad standardization on ES\nModules, this should be mostly fine.\n\n## How it works\n\nSimilar to the async executor provided by\n[`futures-executor`](https://crates.io/crates/futures-executor),\nmultiple worker \"threads\" are spawned upon instantation of the\n`ThreadPool`. Each thread is a web worker, which loads some js glue\ncode. The glue code is provided as a js snippet, which is linked from\nthe wasm-bindgen generated js glue code. This way, it's transparent to\nany bundlers.\nEach web worker is constructed with the following arguments:\n1. WebAssembly module initialization and its shared memory\n([`SharedArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer)).\n2. The third one is a pointer to some shared state, including a channel,\nwhere the async tasks are passed in. The library provides the \n`worker_entry_point` function for this purpose.\n\nOnce the `ThreadPool` is dropped, all channels are closed and the web\nworkers are terminated.\n\nUnfortunately, this requires a nightly compilers because Rust's\nstandard library needs to be recompiled with the following unstable\nfeatures:\n* `atomics`: (rust feature) supports for wasm atomics, see\n  https://github.com/rust-lang/rust/issues/77839\n* `bulk-memory`: (llvm feature) generation of atomic instruction,\n  shared memory, passive segments, etc.\n* `mutable-globals`: (llvm feature)\n\nA note: When workers are destroyed, some memory might be leaked (for\nexample thread local storage or the thead's stack). I recommend\npassing the `--weak-ref` option to `wasm-bindgen` in order to let\nwasm-bindgen create user defined finalizers according to the [WeakRef\nproposal](https://github.com/tc39/proposal-weakrefs).\n\nIn general, implementing wasm threads in Rust seems to have lost some\nof its traction ..\n\n\n## Browser support\n\nSharing memory via\n[`SharedArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer)\nwill soon (starting with Chrome 92) require setting the right headers\nto mark the website as \"cross-origin isolated\", meaning that the\nfollowing headers need to be sent with the main document:\n```\nCross-Origin-Embedder-Policy: require-corp\nCross-Origin-Opener-Policy: same-origin\n```\nFor more information check out [this](https://web.dev/coop-coep/)\nlink.\n\nLoading modules in web workers is currently only supported in\nChromium. There seems the be finally some\n[progress](https://bugzilla.mozilla.org/show_bug.cgi?id=1247687) in\nFirefox. As a workaround, you can include a\n[workers-polyfill](https://unpkg.com/module-workers-polyfill).\n\nIf you're targeting older browser, you might want to do some feature\ndetection and fall back gracefully -- but that's out of scope for this\ndocumentation.\n\n## Is it worth it?\n\nThere is a significant overhead of sending and spawning futures across the\nthread boundary. It makes most sense for long-lived tasks (check out the\n[factorial](./factorial) demo, which is a rough 3x performance increase).\nAs always, make sure to profile your use case.\n\n## Related crates and further information\n\n* The first and foremost resource is the great raytracing demo of the\n[wasm-bindgen\ndocs](https://rustwasm.github.io/wasm-bindgen/examples/raytrace.html)\nand its background [blog\npost](https://rustwasm.github.io/2018/10/24/multithreading-rust-and-wasm.html).\n\n* The\n[wasm-bindgen-rayon](https://github.com/GoogleChromeLabs/wasm-bindgen-rayon)\ncrate provides extensive documentation and a nice end-to-end example.\n\n* The [wasm_thread](https://github.com/chemicstry/wasm_thread) crate\n  is quite similar in its approach as this crate. The main difference\n  is the usage of ES modules.\n\n## License\n\nLicensed under either of\n\n * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)\n * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\nat your option.\n\n#### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be\ndual licensed as above, without any additional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwngr%2Fwasm-futures-executor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwngr%2Fwasm-futures-executor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwngr%2Fwasm-futures-executor/lists"}