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-threads-pool

v2.0.5

Published

Node-Threads-Pool is a tool for creating thread pools in Node.js, making it easy to implement efficient parallel computing. With Node-Threads-Pool, you can easily create and manage multiple threads, allocate tasks to worker threads in the thread pool, and

Downloads

69

Readme

Node-Threads-Pool

Node-Threads-Pool is a Node.js library that provides a simple and efficient way to execute tasks in child threads. It creates a pool of threads that can be used to parallelize the execution of CPU-intensive tasks, making it possible to take advantage of multi-core processors.

The library is designed to be easy to use and to integrate with existing projects. It provides a simple API that allows you to create a thread pool and run tasks in child threads, and it also supports passing data between threads.

node version npm download npm license

Installation

You can install Node-Threads-Pool using NPM:

npm install node-threads-pool

Usage

To use Node-Threads-Pool, you need to create an instance of the Eve class, which represents the thread pool.

// main.js

const { Eve } = require('node-threads-pool');

const tp = new Eve(filename, threadCount);

The Eve constructor takes two arguments: the path to the worker script and the number of threads to create. The run method takes one argument, which is the data to be passed to the worker script.

  • filename: The path to the worker's main script or module. See the Node.js documentation for more details.
  • threadCount: The number of threads in the thread pool.

To run a task in a worker thread, use the run method:

// main.js

tp.run(workerData)
  .then(result => {
    // handle the result
  })
  .catch(err => {
    // handle the error
  })

You can create a new thread directly using the Thread class:

// thread.js

const {Thread} = require('node-threads-pool');

new Thread(async (data) => {
  // Do some work with data
  const result = await doSomething(data);
  return result;
});

The Thread constructor takes a single argument, which is the function to be executed in the child thread. The function should return a Promise that resolves with the result of the computation.

Example

Here are some examples of how to use Node Threads Pool.

Example 1: Running calculations in 20 threads

This example shows how to create a pool of 20 worker threads to perform some calculations:

// main.js
const os = require('os');
const { Eve } = require('node-threads-pool');

const tp = new Eve('thread.js', os.cpus().length);

module.exports = async (data) => {
  return await tp.run(data);
};


// thread.js
const { Thread } = require('node-threads-pool');

const thread = new Thread(async (data) => {
  return await doSomething(data);
});

Alternatively, you can write to the same JS file:

// main.js
const { Eve, Thread, isMainThread } = require('node-threads-pool');
const os = require('os');

if(isMainThread) {

  const tp = new Eve('thread.js', os.cpus().length);

  module.exports = async (data) => {
    return await tp.run(data);
  };

} else {

  new Thread(async (data) => {
    return await doSomething(data);
  });

}

Example 2: Rendering Pug to HTML

This example shows how to use Node Threads Pool to render Pug templates to HTML:

const pug = require('pug');
const os = require('os');
const {Eve, Thread, isMainThread} = require('node-threads-pool');

if(!isMainThread) {
  const options = {};
  new Thread(_data => {
    const {template, data} = _data;
    options.data = data;
    return pug.renderFile(template, options);
  });
} else {
  const tp = new Eve(__filename, os.cpus().length);
  module.exports = async (template, data) => {
    return await tp.run({
      template, data
    });
  };
}

Test

npm run eve

License

Node-Threads-Pool is licensed under the MIT License. (see LICENSE)