Created
June 6, 2024 14:32
-
-
Save ShinJustinHolly3317/da250d1d7a6b2d91b32f6db7ad44db4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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