An easy way to create a pool of worker threads.
If being used with node v10.5.0 to v11.6.0 the worker-thread-pool
module requires node to be started with the --experimental-worker
flag.
//main.js
const Pool = require('worker-thread-pool');
const pool = new Pool({
path: __dirname + '/worker.js'
});
pool.run({name: 'world'})
.then((result) => {
//...
})
//worker.js
const { parentPort } = require('worker_threads');
parentPort.on('message', (message) => {
message.port.postMessage('hello ' + message.name);
message.port.close();
});
Creates a new pool with workers for the specified javascript file.
The path to the javascript file containing the source code to be executed in the thread pool.
The size of the thread pool. Defaults to 4
.
Passes the workerData
to the worker and waits until the worker sends back an answer. Resolves the answer of the worker in a Promise.
Returns the current length of the queue.
Returns the current size of the pool.
Removes all workers from the pool, calls terminate
on them and then emits a close
event�.
If an error occurs during an error
event will be emitted.