Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
node-worker-threads-pool
Advanced tools
Simple worker threads pool using Node's worker_threads module. Compatible with ES6+ Promise, Async/Await.
Simple worker threads pool using Node's worker_threads module. Compatible with ES6+ Promise, Async/Await.
--experimental-worker
flag is added.npm install node-worker-threads-pool --save
Class: StaticPool
Instance of StaticPool is a threads pool with static task provided.
new StaticPool(opt)
opt
<Object>
size
<number>
Number of workers in this pool.task
<string | function>
Static task to do. It can be a absolute path of worker file or a function. Notice: If task is a function, you can not use closure in it! If you do want to use external data in the function, you can use workerData to pass some cloneable data.workerData
<any>
Cloneable data you want to access in task function.staticPool.exec(param)
param
- The param your worker script or task function need.<Promise>
Choose an idle worker in the pool to execute your heavy task with the param you provided. The Promise is resolved with the result.
staticPool.destroy()
Call worker.terminate()
for every worker in the pool and release them.
npm run static-file
// Access the workerData by requiring it.
const { parentPort, workerData } = require("worker_threads");
// Something you shouldn"t run in main thread
// since it will block.
function fib(n) {
if (n < 2) {
return n;
}
return fib(n - 1) + fib (n - 2);
}
// Main thread will pass the data you need
// through this event listener.
parentPort.on("message", (param) => {
if (typeof param !== "number") {
throw new Error("param must be a number.");
}
const result = fib(param);
// Access the workerData.
console.log("workerData is", workerData);
// return the result to main thread.
parentPort.postMessage(result);
});
const { StaticPool } = require("node-worker-threads-pool");
const filePath = "absolute/path/to/your/worker/script";
const pool = new StaticPool({
size: 4,
task: filePath,
workerData: "workerData!"
});
for (let i = 0; i < 20; i++) {
(async () => {
const num = 40 + Math.trunc(10 * Math.random());
// This will choose one idle worker in the pool
// to execute your heavy task without blocking
// the main thread!
const res = await pool.exec(num);
console.log(`Fibonacci(${num}) result:`, res);
})();
}
npm run static-function
const { StaticPool } = require("node-worker-threads-pool");
const pool = new StaticPool({
size: 4,
task: function(n) {
const num = this.workerData.num;
for (let i = 0; i < num; i++) {
n += i;
}
return n;
},
workerData: {
num: 1 << 30
}
});
for (let i = 0; i < 20; i++) {
(async () => {
const res = await pool.exec(i);
console.log(`result${i}:`, res);
})();
}
Class: DynamicPool
Instance of DynamicPool is a threads pool executes different task functions provided every call.
new DynamicPool(size)
size
<number>
Number of workers in this pool.dynamicPool.exec(opt)
opt
task
<function>
Function as a task to do. Notice: You can not use closure in task function! If you do want to use external data in the function, you can use workerData to pass some cloneable data.workerData
<any>
Cloneable data you want to access in task function.<Promise>
Choose one idle worker in the pool to execute your task function. The Promise is resolved with the result your task returned.
dynamicPool.destroy()
Call worker.terminate()
for every worker in the pool and release them.
npm run dynamic
const { DynamicPool } = require("node-worker-threads-pool");
const pool = new DynamicPool(4);
function task1() {
// something heavy.
}
function task2() {
// something heavy too.
}
// execute task1
(async () => {
const res = await pool.exec({
task: task1,
workerData: {
// some data
}
});
console.log(res);
})();
// execute task2
(async () => {
const res = await pool.exec({
task: task2,
workerData: {
// some data
}
});
console.log(res);
})();
FAQs
Simple worker threads pool using Node's worker_threads module. Compatible with ES6+ Promise, Async/Await.
The npm package node-worker-threads-pool receives a total of 15,511 weekly downloads. As such, node-worker-threads-pool popularity was classified as popular.
We found that node-worker-threads-pool demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.