npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-workers-pool

v1.1.4

Published

Easy way to manage a pool of worker threads.

Downloads

31

Readme

node-workers-pool

Easy way to manage a pool of worker threads.

In Nodejs v12.14 LTS or later, the --experimental-worker flag is not necessary anymore, since this resource become stable.

In Nodejs up to 11.x, use the --experimental-worker flag to run correctly, since this resource still experimental.

Introduction

With this package you can:

  • Run heavy cpu-bound in a pool of worker_threads, an experimental resouce in NodeJs.
  • Control the number of active workers in the pool.
  • Create a queue, because when all the workers are busy, new processing requests will be queued.
  • Easily clear the pool (workers and queue).
  • Easily capture the result or error from the workers, since the pool uses Promise.

Prerequisites

  • NodeJs (v 12.x or later for stable / v 10.15.0 up to v 11.x for experimental)
  • Npm

DEV - Prerequisites

Examples

  • Creating a pool with max 10 workers.
const pool1 = require('node-workers-pool')({
    max: 10,
    queueMax: 40
});

--or

const Pool = require('node-workers-pool');
const pool2 = Pool({ max: 10, queueMax: 40 });
  • If options is missing, the max number of workers will be the core's number of the machine (require('os').cpus().length).

  • Creating a task and sending to queue

const now = new Date();
const pool = require('node-workers-pool')({ max: 4, queueMax: 10 });

              /* Function to execute*/          /* Params */
pool.enqueue((num1, num2) => num1 + num2,       15, 30)
.then(result => console.log(`Executed in ${(new Date() - now) / 1000} sec(s). Result: ${result}`))
.catch(err => console.err(err));
  • Another example
const now = new Date();
const pool = require('node-workers-pool')({ max: 4, queueMax: 10 });

function fibonacci(num) {
    if (num < 2){
        return num
    }
    return fibonacci(num - 1) + fibonacci(num - 2);
}

pool.enqueue(fibonnaci, 40) // Function and parameter
.then(result => console.log(`Executed in ${(new Date() - now) / 1000} sec(s). Result: ${result}`))
.catch(err => console.err(err));
  • Multiple parameters example
const pool = require('node-workers-pool')({ max: 4, queueMax: 10 });

function mult(num1, num2, num3, num4) {
    return num1 * num2 * num3 * num4;
}

pool.enqueue(mult, 3, 5, 8, 10) // Function and parameters
.then(result => {
    console.log(`Executed in ${(new Date() - now) / 1000} sec(s). Result: ${result}`);
    pool.finishPool()
    .then(() => console.log('Finished!'));
})
.catch(err => console.err(err));
  • Cleaning the pool
const pool = require('node-workers-pool')({ max: 4, queueMax: 10 });

pool.enqueue(() => 'Executed') // Function and parameters
.then(result => {
    console.log(result);
    pool.finishPool()
    .then(() => console.log('Pool finished!'));
});
  • Default values
const Pool = require('node-workers-pool');
const pool = new Pool(); // without opts

/** This pool will be configured by default with:
*   max = require('os').cpus().length
*   queueMax = 10
*/

Tests

Obs: Express processes identical requests one after another. Performing the same get from the same host with the same parameters can make it not run asynchronously, due to the express.

Run the code below to test:

npm test

This will start an express server with 3 async routes and 3 sync routes. You can test all of them in your browser and see that sync routes will lock all the others, while async routes will allow other request, because they don't lock the Event Loop.

Examples:

  • Event loop free (async):
// Tab 1 - run:
http://127.0.0.1:3000/asyncsum/10000000000

// Tab 2 - run:
http://127.0.0.1:3000/asyncsum/10
// the second request will answer even while the first still running.
  • Event loop blocked (sync):
// Tab 1 - run:
http://127.0.0.1:3000/syncfibo/44

// Tab 2 - run:
http://127.0.0.1:3000/asyncfibo/5
// the second request will wait the first finish, because the fist is running in the event loop.

Notes

  • Case the pool and queue are full, an error 'full' will be thrown.
  • The workers will be allocated as needed, so just create a pool will not create all workers at same time.

Click here to read more about worker_threads.

Author

License

The project is licensed under the MIT License. See the LICENSE file for more details