A worker thread pool for CPU-bound tasks. It requires no configuration and has many powerful features:
- Generator and AsyncGenerator function support
- Worker functions can be generator functions or async generator functions, making it easy to stream results back to the main thread. Iteration happens eagerly, to maximize parallelism (i.e., the main thread does not pause the generator function).
- Functions (callbacks) as arguments
- Functions can be passed to worker tasks. They become
asyncfunctions in the worker thread, using MessagePort for communication under the hood.
- Functions can be passed to worker tasks. They become
- Cancellation support
- Tasks can be aborted using an AbortSignal.
- Automatic thread management
- Crashed threads are automatically respawned, unless they're crashing during startup (to prevent an infinite spawn loop).
- Zero-copy data transfer
- Data can be efficiently moved between threads without copying it. This is beneficial to performance when passing large Buffers between threads.
npm install wise-workers
Requires Node.js v16.x.x or later.
const ThreadPool = require('wise-workers');
const pool = new ThreadPool({ filename: require.resolve('./worker') });
const result = await pool.call('add',