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.
npm install node-worker-threads-pool --save
npm run example
const { parentPort } = require('worker_threads');
// Something you shouldn't run in main thread
// since it will block the main thread.
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);
// return the result to main thread.
parentPort.postMessage(result);
});
const Pool = require('node-worker-threads-pool');
const filePath = 'absolute/path/to/your/worker/script';
const num = 4; // The number of workers in this pool.
const pool = new Pool(filePath, num);
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);
})();
}
new Pool(filePath, num)
filePath
- The absolute path of your worker.js file.num
- The number of the workers in your pool.pool.exec(data)
data
- The data your worker script need.<Promise>
Choose one idle worker in the pool to execute your heavy task with the data you provided. The Promise is resolved with the result your worker generated.
pool.destroy()
Call worker.terminate()
for every worker in the pool and release them.
node-worker-threads-pool is licensed under the GNU General Public License v3 (GPL-3) (http://www.gnu.org/copyleft/gpl.html).
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.