worker-thread-pool

A easy way to create a pool of worker threads.

Usage no npm install needed!

<script type="module">
  import workerThreadPool from 'https://cdn.skypack.dev/worker-thread-pool';
</script>

README

worker-thread-pool

Build Status Coverage Status JavaScript Style Guide NPM version Greenkeeper badge

An easy way to create a pool of worker threads.

Usage

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();
});

API

Pool(options)

Creates a new pool with workers for the specified javascript file.

options

path

The path to the javascript file containing the source code to be executed in the thread pool.

size (optional)

The size of the thread pool. Defaults to 4.

Pool#run(workerData)

Passes the workerData to the worker and waits until the worker sends back an answer. Resolves the answer of the worker in a Promise.

Poll#queueLength()

Returns the current length of the queue.

Poll#poolLength()

Returns the current size of the pool.

Pool#close()

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.

License

MIT