Gathering detailed insights and metrics for workerpool
Gathering detailed insights and metrics for workerpool
Gathering detailed insights and metrics for workerpool
Gathering detailed insights and metrics for workerpool
Offload tasks to a pool of workers on node.js and in the browser
npm install workerpool
68.6
Supply Chain
99.2
Quality
86.1
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,098 Stars
450 Commits
146 Forks
24 Watching
1 Branches
42 Contributors
Updated on 24 Nov 2024
Minified
Minified + Gzipped
JavaScript (97.52%)
TypeScript (2.48%)
Cumulative downloads
Total Downloads
Last day
-11.8%
1,556,216
Compared to previous day
Last week
3%
9,527,765
Compared to previous week
Last month
19%
37,874,845
Compared to previous month
Last year
23.8%
350,426,233
Compared to previous year
workerpool offers an easy way to create a pool of workers for both dynamically offloading computations as well as managing a pool of dedicated workers. workerpool basically implements a thread pool pattern. There is a pool of workers to execute tasks. New tasks are put in a queue. A worker executes one task at a time, and once finished, picks a new task from the queue. Workers can be accessed via a natural, promise based proxy, as if they are available straight in the main application.
workerpool runs on Node.js and in the browser.
JavaScript is based upon a single event loop which handles one event at a time. Jeremy Epstein explains this clearly:
In Node.js everything runs in parallel, except your code. What this means is that all I/O code that you write in Node.js is non-blocking, while (conversely) all non-I/O code that you write in Node.js is blocking.
This means that CPU heavy tasks will block other tasks from being executed. In case of a browser environment, the browser will not react to user events like a mouse click while executing a CPU intensive task (the browser "hangs"). In case of a node.js server, the server will not respond to any new request while executing a single, heavy request.
For front-end processes, this is not a desired situation. Therefore, CPU intensive tasks should be offloaded from the main event loop onto dedicated workers. In a browser environment, Web Workers can be used. In node.js, child processes and worker_threads are available. An application should be split in separate, decoupled parts, which can run independent of each other in a parallelized way. Effectively, this results in an architecture which achieves concurrency by means of isolated processes and message passing.
Install via npm:
npm install workerpool
To load workerpool in a node.js application (both main application as well as workers):
1const workerpool = require('workerpool');
To load workerpool in the browser:
1<script src="workerpool.js"></script>
To load workerpool in a web worker in the browser:
1importScripts('workerpool.js');
Setting up the workerpool with React or webpack5 requires additional configuration steps, as outlined in the webpack5 section.
In the following example there is a function add
, which is offloaded dynamically to a worker to be executed for a given set of arguments.
myApp.js
1const workerpool = require('workerpool'); 2const pool = workerpool.pool(); 3 4function add(a, b) { 5 return a + b; 6} 7 8pool 9 .exec(add, [3, 4]) 10 .then(function (result) { 11 console.log('result', result); // outputs 7 12 }) 13 .catch(function (err) { 14 console.error(err); 15 }) 16 .then(function () { 17 pool.terminate(); // terminate all workers when done 18 });
Note that both function and arguments must be static and stringifiable, as they need to be sent to the worker in a serialized form. In case of large functions or function arguments, the overhead of sending the data to the worker can be significant.
A dedicated worker can be created in a separate script, and then used via a worker pool.
myWorker.js
1const workerpool = require('workerpool'); 2 3// a deliberately inefficient implementation of the fibonacci sequence 4function fibonacci(n) { 5 if (n < 2) return n; 6 return fibonacci(n - 2) + fibonacci(n - 1); 7} 8 9// create a worker and register public functions 10workerpool.worker({ 11 fibonacci: fibonacci, 12});
This worker can be used by a worker pool:
myApp.js
1const workerpool = require('workerpool'); 2 3// create a worker pool using an external worker script 4const pool = workerpool.pool(__dirname + '/myWorker.js'); 5 6// run registered functions on the worker via exec 7pool 8 .exec('fibonacci', [10]) 9 .then(function (result) { 10 console.log('Result: ' + result); // outputs 55 11 }) 12 .catch(function (err) { 13 console.error(err); 14 }) 15 .then(function () { 16 pool.terminate(); // terminate all workers when done 17 }); 18 19// or run registered functions on the worker via a proxy: 20pool 21 .proxy() 22 .then(function (worker) { 23 return worker.fibonacci(10); 24 }) 25 .then(function (result) { 26 console.log('Result: ' + result); // outputs 55 27 }) 28 .catch(function (err) { 29 console.error(err); 30 }) 31 .then(function () { 32 pool.terminate(); // terminate all workers when done 33 });
Worker can also initialize asynchronously:
myAsyncWorker.js
1define(['workerpool/dist/workerpool'], function (workerpool) { 2 // a deliberately inefficient implementation of the fibonacci sequence 3 function fibonacci(n) { 4 if (n < 2) return n; 5 return fibonacci(n - 2) + fibonacci(n - 1); 6 } 7 8 // create a worker and register public functions 9 workerpool.worker({ 10 fibonacci: fibonacci, 11 }); 12});
Examples are available in the examples directory:
https://github.com/josdejong/workerpool/tree/master/examples
The API of workerpool consists of two parts: a function workerpool.pool
to create a worker pool, and a function workerpool.worker
to create a worker.
A workerpool can be created using the function workerpool.pool
:
workerpool.pool([script: string] [, options: Object]) : Pool
When a script
argument is provided, the provided script will be started as a dedicated worker. When no script
argument is provided, a default worker is started which can be used to offload functions dynamically via Pool.exec
. Note that on node.js, script
must be an absolute file path like __dirname + '/myWorker.js'
. In a browser environment, script
can also be a data URL like 'data:application/javascript;base64,...'
. This allows embedding the bundled code of a worker in your main application. See examples/embeddedWorker
for a demo.
The following options are available:
minWorkers: number | 'max'
. The minimum number of workers that must be initialized and kept available. Setting this to 'max'
will create maxWorkers
default workers (see below).maxWorkers: number
. The default number of maxWorkers is the number of CPU's minus one. When the number of CPU's could not be determined (for example in older browsers), maxWorkers
is set to 3.maxQueueSize: number
. The maximum number of tasks allowed to be queued. Can be used to prevent running out of memory. If the maximum is exceeded, adding a new task will throw an error. The default value is Infinity
.workerType: 'auto' | 'web' | 'process' | 'thread'
.
'auto'
(default), workerpool will automatically pick a suitable type of worker: when in a browser environment, 'web'
will be used. When in a node.js environment, worker_threads
will be used if available (Node.js >= 11.7.0), else child_process
will be used.'web'
, a Web Worker will be used. Only available in a browser environment.'process'
, child_process
will be used. Only available in a node.js environment.'thread'
, worker_threads
will be used. If worker_threads
are not available, an error is thrown. Only available in a node.js environment.workerTerminateTimeout: number
. The timeout in milliseconds to wait for a worker to cleanup it's resources on termination before stopping it forcefully. Default value is 1000
.abortListenerTimeout: number
. The timeout in milliseconds to wait for abort listener's before stopping it forcefully, triggering cleanup. Default value is 1000
.forkArgs: String[]
. For process
worker type. An array passed as args
to child_process.forkforkOpts: Object
. For process
worker type. An object passed as options
to child_process.fork. See nodejs documentation for available options.workerOpts: Object
. For web
worker type. An object passed to the constructor of the web worker. See WorkerOptions specification for available options.workerThreadOpts: Object
. For worker
worker type. An object passed to worker_threads.options. See nodejs documentation for available options.onCreateWorker: Function
. A callback that is called whenever a worker is being created. It can be used to allocate resources for each worker for example. The callback is passed as argument an object with the following properties:
forkArgs: String[]
: the forkArgs
option of this poolforkOpts: Object
: the forkOpts
option of this poolworkerOpts: Object
: the workerOpts
option of this poolscript: string
: the script
option of this pool
Optionally, this callback can return an object containing one or more of the above properties. The provided properties will be used to override the Pool properties for the worker being created.onTerminateWorker: Function
. A callback that is called whenever a worker is being terminated. It can be used to release resources that might have been allocated for this specific worker. The callback is passed as argument an object as described for onCreateWorker
, with each property sets with the value for the worker being terminated.emitStdStreams: boolean
. For process
or thread
worker type. If true
, the worker will emit stdout
and stderr
events instead of passing it through to the parent streams. Default value is false
.Important note on
'workerType'
: when sending and receiving primitive data types (plain JSON) from and to a worker, the different worker types ('web'
,'process'
,'thread'
) can be used interchangeably. However, when using more advanced data types like buffers, the API and returned results can vary. In these cases, it is best not to use the'auto'
setting but have a fixed'workerType'
and good unit testing in place.
A worker pool contains the following functions:
Pool.exec(method: Function | string, params: Array | null [, options: Object]) : Promise<any, Error>
Execute a function on a worker with given arguments.
method
is a string, a method with this name must exist at the worker and must be registered to make it accessible via the pool. The function will be executed on the worker with given parameters.method
is a function, the provided function fn
will be stringified, send to the worker, and executed there with the provided parameters. The provided function must be static, it must not depend on variables in a surrounding scope.Pool.proxy() : Promise<Object, Error>
Create a proxy for the worker pool. The proxy contains a proxy for all methods available on the worker. All methods return promises resolving the methods result.
Pool.stats() : Object
Retrieve statistics on workers, and active and pending tasks.
Returns an object containing the following properties:
{
totalWorkers: 0,
busyWorkers: 0,
idleWorkers: 0,
pendingTasks: 0,
activeTasks: 0
}
Pool.terminate([force: boolean [, timeout: number]]) : Promise<void, Error>
If parameter force
is false (default), workers will finish the tasks they are working on before terminating themselves. Any pending tasks will be rejected with an error 'Pool terminated'. When force
is true, all workers are terminated immediately without finishing running tasks. If timeout
is provided, worker will be forced to terminate when the timeout expires and the worker has not finished.
The function Pool.exec
and the proxy functions all return a Promise
. The promise has the following functions available:
Promise.then(fn: Function<result: any>) : Promise<any, Error>
Promise.catch(fn: Function<error: Error>) : Promise<any, Error>
Promise.finally(fn: Function<void>)
resolves
or rejects
Promise.cancel() : Promise<any, Error>
Promise.CancellationError
.Promise.timeout(delay: number) : Promise<any, Error>
Promise.TimeoutError
.Example usage:
1const workerpool = require('workerpool'); 2 3function add(a, b) { 4 return a + b; 5} 6 7const pool1 = workerpool.pool(); 8 9// offload a function to a worker 10pool1 11 .exec(add, [2, 4]) 12 .then(function (result) { 13 console.log(result); // will output 6 14 }) 15 .catch(function (err) { 16 console.error(err); 17 }); 18 19// create a dedicated worker 20const pool2 = workerpool.pool(__dirname + '/myWorker.js'); 21 22// supposed myWorker.js contains a function 'fibonacci' 23pool2 24 .exec('fibonacci', [10]) 25 .then(function (result) { 26 console.log(result); // will output 55 27 }) 28 .catch(function (err) { 29 console.error(err); 30 }); 31 32// send a transferable object to the worker 33// supposed myWorker.js contains a function 'sum' 34const toTransfer = new Uint8Array(2).map((_v, i) => i) 35pool2 36 .exec('sum', [toTransfer], { transfer: [toTransfer.buffer] }) 37 .then(function (result) { 38 console.log(result); // will output 3 39 }) 40 .catch(function (err) { 41 console.error(err); 42 }); 43 44// create a proxy to myWorker.js 45pool2 46 .proxy() 47 .then(function (myWorker) { 48 return myWorker.fibonacci(10); 49 }) 50 .then(function (result) { 51 console.log(result); // will output 55 52 }) 53 .catch(function (err) { 54 console.error(err); 55 }); 56 57// create a pool with a specified maximum number of workers 58const pool3 = workerpool.pool({ maxWorkers: 7 });
A worker is constructed as:
workerpool.worker([methods: Object<String, Function>] [, options: Object]) : void
Argument methods
is optional and can be an object with functions available in the worker. Registered functions will be available via the worker pool.
The following options are available:
onTerminate: ([code: number]) => Promise<void> | void
. A callback that is called whenever a worker is being terminated. It can be used to release resources that might have been allocated for this specific worker. The difference with pool's onTerminateWorker
is that this callback runs in the worker context, while onTerminateWorker
is executed on the main thread.Example usage:
1// file myWorker.js 2const workerpool = require('workerpool'); 3 4function add(a, b) { 5 return a + b; 6} 7 8function multiply(a, b) { 9 return a * b; 10} 11 12// create a worker and register functions 13workerpool.worker({ 14 add: add, 15 multiply: multiply, 16});
Asynchronous results can be handled by returning a Promise from a function in the worker:
1// file myWorker.js 2const workerpool = require('workerpool'); 3 4function timeout(delay) { 5 return new Promise(function (resolve, reject) { 6 setTimeout(resolve, delay); 7 }); 8} 9 10// create a worker and register functions 11workerpool.worker({ 12 timeout: timeout, 13});
Transferable objects can be sent back to the pool using Transfer
helper class:
1// file myWorker.js 2const workerpool = require('workerpool'); 3 4function array(size) { 5 var array = new Uint8Array(size).map((_v, i) => i); 6 return new workerpool.Transfer(array, [array.buffer]); 7} 8 9// create a worker and register functions 10workerpool.worker({ 11 array: array, 12});
You can send data back from workers to the pool while the task is being executed using the workerEmit
function:
workerEmit(payload: any) : unknown
This function only works inside a worker and during a task.
Example:
1// file myWorker.js 2const workerpool = require('workerpool'); 3 4function eventExample(delay) { 5 workerpool.workerEmit({ 6 status: 'in_progress', 7 }); 8 9 workerpool.workerEmit({ 10 status: 'complete', 11 }); 12 13 return true; 14} 15 16// create a worker and register functions 17workerpool.worker({ 18 eventExample: eventExample, 19});
To receive those events, you can use the on
option of the pool exec
method:
1pool.exec('eventExample', [], { 2 on: function (payload) { 3 if (payload.status === 'in_progress') { 4 console.log('In progress...'); 5 } else if (payload.status === 'complete') { 6 console.log('Done!'); 7 } 8 }, 9});
Workers have access to a worker
api which contains the following methods
emit: (payload: unknown | Transfer): void
addAbortListener: (listener: () => Promise<void>): void
Worker termination may be recoverable through abort listeners
which are registered through worker.addAbortListener
. If all registered listeners resolve then the worker will not be terminated, allowing for worker reuse in some cases.
NOTE: For operations to successfully clean up, a worker implementation should be async. If the worker thread is blocked, then the worker will be killed.
1function asyncTimeout() { 2 var me = this; 3 return new Promise(function (resolve) { 4 let timeout = setTimeout(() => { 5 resolve(); 6 }, 5000); 7 8 // Register a listener which will resolve before the time out 9 // above triggers. 10 me.worker.addAbortListener(async function () { 11 clearTimeout(timeout); 12 resolve(); 13 }); 14 }); 15} 16 17// create a worker and register public functions 18workerpool.worker( 19 { 20 asyncTimeout: asyncTimeout, 21 }, 22 { 23 abortListenerTimeout: 1000 24 } 25);
Events may also be emitted from the worker
api through worker.emit
1// file myWorker.js 2const workerpool = require('workerpool'); 3 4function eventExample(delay) { 5 this.worker.emit({ 6 status: "in_progress", 7 }); 8 workerpool.workerEmit({ 9 status: 'complete', 10 }); 11 12 return true; 13} 14 15// create a worker and register functions 16workerpool.worker({ 17 eventExample: eventExample, 18});
Following properties are available for convenience:
map
, reduce
, forEach
,
filter
, some
, every
, ...First clone the project from github:
git clone git://github.com/josdejong/workerpool.git
cd workerpool
Install the project dependencies:
npm install
Then, the project can be build by executing the build script via npm:
npm run build
This will build the library workerpool.js and workerpool.min.js from the source files and put them in the folder dist.
To execute tests for the library, install the project dependencies once:
npm install
Then, the tests can be executed:
npm test
To test code coverage of the tests:
npm run coverage
To see the coverage results, open the generated report in your browser:
./coverage/index.html
npm install
to update it in package-lock.json
too.npm publish
.git tag v1.2.3
git push --tags
Copyright (C) 2014-2024 Jos de Jong wjosdejong@gmail.com
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
21 commit(s) and 6 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 7/24 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More