Skip to content

Instantly share code, notes, and snippets.

@ShinJustinHolly3317
Created June 6, 2024 14:32
Show Gist options
  • Save ShinJustinHolly3317/da250d1d7a6b2d91b32f6db7ad44db4c to your computer and use it in GitHub Desktop.
Save ShinJustinHolly3317/da250d1d7a6b2d91b32f6db7ad44db4c to your computer and use it in GitHub Desktop.
const { Worker } = require('worker_threads');
const path = require('path');
function runService(workerData) {
return new Promise((resolve, reject) => {
const worker = new Worker(path.resolve(__dirname, './worker.js'), { workerData });
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
});
}
const numWorkers = 100;
const promises = [];
for (let i = 0; i < numWorkers; i++) {
promises.push(runService());
}
Promise.all(promises)
.then(results => {
results.forEach(result => {
console.log(result);
});
})
.catch(err => {
console.error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment