node-worker-pool
Offload heavy tasks to a pool of worker threads.
Requires Node.js 10.5.0 and up.
npm install @intrnl/node-worker-pool
# pnpm install @intrnl/node-worker-pool
# yarn add @intrnl/node-worker-pool
Usage
// ./app.js
let { WorkerPool } = require('@intrnl/node-worker-pool');
// Path to your worker script, `require.resolve` will resolve requires from
// where this script is located.
let workerScript = require.resolve('./worker.js');
let pool = new WorkerPool(workerScript, {
// Max workers to run concurrently, default is number of CPU cores minus 1
max: 2,
// Resource limits to set on the worker thread, Node.js 12.16.0 and up only,
// see <https://nodejs.org/api/worker_threads.html#worker_threads_worker_resourcelimits>
limits: {},
});
pool.exec('fibonacci', [32])
// This should output `2178309`
.then((value) => console.log('result:', value))
.catch((error) => console.error('error:', error))
// Wait for all ongoing tasks to complete before terminating the worker
.then(() => pool.close())
// ./worker.js
function fibonacci (n) {
if (n < 2) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
module.exports = { fibonacci };
API Reference
This module exports two classes, the pool and the handler used to manage the actual worker thread.
WorkerPool
Constructor options
-
max?: number
The maximum amount of workers that the pool should create, default is your CPU cores minus 1. -
limits?: NodeJS.ResourceLimits
The resource limits that the worker should be allowed to use, see this page for details
workerPool.exec(method: string, args: any[]): Promise<any>
Executes a method exported by the worker, along with arguments to be passed to the method.
workerPool.close(opts?: WorkerPoolCloseOptions): Promise<void>
Closes the pool while waiting for ongoing tasks to finish before terminating the workers. Queued tasks are immediately thrown.
WorkerPoolCloseOptions
-
timeout?: false | number
How long the pool should wait for ongoing tasks to complete before terminating the workers, default isfalse
which disables timeout and waits for ongoing tasks to complete entirely.
workerPool.terminate(): Promise<void>
Closes the pool and forcefully terminates all the workers. Ongoing and queued tasks are immediately thrown.