Skip to content

Node-Threads-Pool: A Node.js library for efficient task execution in child threads, with a thread pool for CPU-intensive tasks and multi-core processing.

License

Notifications You must be signed in to change notification settings

pxgo/node-threads-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

node-threads-pool

Install

npm install node-threads-pool

Usage


//app.js

const TP = require("node-threads-pool");
const tp = new TP(5);

tp.run("./thread.js", {
  workerData: {
    n: 40
  }
})
  .then(result => {
    console.log(result);
  })
  .catch(err => {
    console.error(err);
  })
// thread.js

const {workerData, parentPort} = require("worker_threads");
const {n} = workerData;
const fn = (n) => {
  if(n < 3) return 1;
  return fn(n-2) + fn(n-1);
}
parentPort.postMessage(fn(n));

Methods

new TP([number]); // number: the maximum number of threads. The default value is 1.

Create a thread pool instance.

TP.run(filename[, options]); // options: View here

Run a file in a child thread.

About

Node-Threads-Pool: A Node.js library for efficient task execution in child threads, with a thread pool for CPU-intensive tasks and multi-core processing.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published