@zebrajaeger/threadpool
v1.0.0
Published
js threadpool
Downloads
6
Readme
threadpool
js threadpool
Example
Pool Code
const path = require('path')
const {StaticWorkerPool} = require('@zebrajaeger/threadpool')
const threadFile = path.resolve(__dirname, 'mythread.js');
(async () => {
// create pool (default count = os.cpus().length)
const p = new StaticWorkerPool(threadFile).begin();
// start 20 jobs
for (let i = 1; i <= 20; ++i) {
p.exec(`foo(${i})`).promise.then(r => console.log('xxx', r))
}
// wait until all jobs are done
await p.finished();
console.log('DONE')
// shutdown pool
await p.destroy();
console.log('Destroyed')
})();
Worker Code (mythread.js)
const {parentPort} = require('worker_threads');
// for every worker this is a new instance
// so this 'counter' starts everytime with zero.
let counter = 0;
// the code to execute:
// - wait a little bit and then return result
// - on every 5th execution fail
async function toExec(msg) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (++counter >= 5) {
counter = 0;
reject('Oh no!!');
} else {
resolve(`Meep(${counter}): '${msg}'`);
}
}, 100)
})
}
// entry point for WorkerPool/WorkerThread
parentPort.on('message', async data => {
// wrap errors. Not needed but more easy to handle
try {
parentPort.postMessage({result: await toExec(data), data});
} catch (error) {
parentPort.postMessage({error, data});
}
});