worker-threads-pool
Easily manage a pool of Node.js Worker
Threads.Installation
npm install worker-threads-pool --save
Prerequisites
Worker Threads in Node.js are still an experimental feature and is only supported in Node.js v10.5.0 and above. To use Worker Threads, you need to runnode
with the --experimental-worker
flag:node --experimental-worker app.js
Usage
const Pool = require('worker-threads-pool')
const pool = new Pool({max: 5})
for (let i = 0; i < 100; i++) {
pool.acquire('/my/worker.js', function (err, worker) {
if (err) throw err
console.log(`started worker ${i} (pool size: ${pool.size})`)
worker.on('exit', function () {
console.log(`worker ${i} exited (pool size: ${pool.size})`)
})
})
}
API
pool = new Pool([options])
options
is an optional object/dictionary with the any of the following properties:max
- Maximum number of workers allowed in the pool. Other workers
1
)maxWaiting
- Maximum number of workers waiting to be started when
pool.acquire
will be called with
an error in case this limit is reachedpool.size
Number of active workers in the pool.pool.acquire(filename[, options], callback)
The filename
and options
arguments are passed directly to `new
Worker(filename,
options)`.The
callback
argument will be called with the an optional error object
and the worker once it's created.pool.destroy([callback])
Calls
worker.terminate()
on all workers in the pool.Will call the optional
callback
once all workers have terminated.