You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`wasmworker` is a library that provides easy access to parallelization on web targets when compiled to WebAssembly using [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen).
3
10
In contrast to many other libraries like [wasm-bindgen-rayon](https://github.com/RReverser/wasm-bindgen-rayon), this library does not require SharedArrayBuffer support.
4
11
@@ -10,20 +17,18 @@ In contrast to many other libraries like [wasm-bindgen-rayon](https://github.com
10
17
-[Iterator extension](#iterator-extension)
11
18
-[Async functions with channels](#async-functions-with-channels)
12
19
-[Bundler support (Vite)](#bundler-support-vite)
20
+
-[Idle timeout](#idle-timeout)
13
21
-[FAQ](#faq)
14
22
15
23
## Usage
16
-
The library consists of two crates:
17
-
-`wasmworker`: The main crate that also offers access to the webworker, as well as the worker pool and iterator extensions.
18
-
-`wasmworker-proc-macro`: This crate is needed to expose functions towards the web workers via the `#[webworker_fn]` macro.
19
24
20
25
### Setting up
21
-
To use this library, include both dependencies to your `Cargo.toml`.
26
+
To use this library, add the following dependency to your `Cargo.toml`.
27
+
Enable the `macros` feature to get access to the `#[webworker_fn]` and `#[webworker_channel_fn]` attribute macros.
22
28
23
29
```toml
24
30
[dependencies]
25
-
wasmworker = "0.2"
26
-
wasmworker-proc-macro = "0.2"
31
+
wasmworker = { version = "0.3", features = ["macros"] }
27
32
```
28
33
29
34
The `wasmworker` crate comes with a default feature called `serde`, which allows running any function on a web worker under the following two conditions:
@@ -35,7 +40,7 @@ This is useful for users that do not want a direct serde dependency. Internally,
35
40
36
41
You can then start using the library without further setup.
37
42
If you plan on using the global `WebWorkerPool` (using the iterator extensions or `worker_pool()`), you can *optionally* configure this pool:
38
-
```rust,no_run
43
+
```rust
39
44
// Importing it publicly will also expose the function on the JavaScript side.
40
45
// You can instantiate the pool both via Rust and JS.
options.num_workers =Some(2); // Default is navigator.hardwareConcurrency
46
51
init_worker_pool(options).await.expect("Worker pool already initialized");
47
52
}
48
-
# fn main() {}
49
53
```
50
54
51
55
### Outsourcing tasks
@@ -55,11 +59,12 @@ The library offers three ways of outsourcing function calls onto concurrent work
55
59
3.`par_map`: an extension to regular iterators, which allows to execute a function on every element of the iterator in parallel using the default worker pool.
56
60
57
61
All approaches require the functions that should be executed to be annotated with the `#[webworker_fn]` macro.
58
-
This macro ensures that the functions are available to the web worker instances:
62
+
This macro ensures that the functions are available to the web worker instances.
63
+
To execute such a function, pass its `WebWorkerFn` handle (obtained via the `webworker!()` macro) to a worker:
Most of the time, we probably want to schedule tasks to a pool of workers, though.
115
106
The default worker pool is instantiated on first use and can be configured using `init_worker_pool()` as described above.
116
107
It uses a round-robin scheduler (with the second option being a load based scheduler), a number of `navigator.hardwareConcurrency` separate workers, and the default inferred path.
Workers can be automatically terminated after a period of inactivity and transparently recreated when new tasks arrive. This is useful for freeing resources in applications where worker usage is intermittent:
0 commit comments