Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

docs: update fan-out example to use assign() and run.workers

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

+8 -3
+8 -3
README.md
··· 327 327 When you pass the same `AsyncIterable` or `StreamTask` argument to multiple tasks, each task gets its own copy of the data. Use `channel()` to share a single source across multiple workers — each item goes to exactly one consumer (work stealing). 328 328 329 329 ```ts 330 - import { workers, channel, mo } from 'moroutine'; 330 + import { workers, channel, assign, mo } from 'moroutine'; 331 331 332 332 const generate = mo(import.meta, async function* (n: number) { 333 333 for (let i = 0; i < n; i++) yield i; ··· 344 344 const ch = channel(generate(100)); 345 345 346 346 { 347 - using run = workers(4); 348 - const [a, b, c, d] = await run([process(ch), process(ch), process(ch), process(ch)]); 347 + using run = workers(); 348 + const fanout = run.workers.map((w) => { 349 + return assign(w, process(ch)); 350 + }); 351 + const results = await run(fanout); 349 352 // Items distributed across workers — no duplicates, no gaps 350 353 } 351 354 ``` 355 + 356 + Use `assign(worker, task)` to pin a task to a specific worker. `run.workers` is a read-only array of worker handles, one per pool worker. 352 357 353 358 Without `channel()`, `AsyncIterable` and `StreamTask` arguments are auto-detected and streamed to a single consumer. `channel()` is only needed for fan-out. 354 359