npm install node-threads-pool
//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));
Create a thread pool instance.
TP.run(filename[, options]); // options: View here
Run a file in a child thread.