Last active
April 8, 2020 08:04
-
-
Save srebalaji/8d6a29f84a4389365b68f0dbc82e5781 to your computer and use it in GitHub Desktop.
Worker thread pool - Manage worker threads pool in NodeJS
This file contains hidden or 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
// workerPool.js | |
// Worker Thread pool | |
const {Worker, parentPort, MessageChannel} = require('worker_threads') | |
class WorkerPool { | |
constructor(size, worker) { | |
this.size = size | |
this.worker = worker | |
this.pool = [] | |
} | |
getWorker() { | |
if (this.size > this.pool.length) { | |
const worker = new Worker(this.worker) | |
this.pool.push(worker) | |
return worker | |
} else { | |
return this.pool[Math.floor(Math.random() * this.size)] | |
} | |
} | |
execute(...data) { | |
return new Promise((resolve, reject) => { | |
const worker = this.getWorker() | |
const { port1, port2 } = new MessageChannel() | |
worker.postMessage({ data, port: port1 }, [port1]) | |
port2.on('message', (result) => { | |
resolve(result) | |
}) | |
port2.on('error', (err) => { | |
reject(err) | |
}) | |
port2.on('close', (code) => { | |
this.pool = this.pool.filter(p => p !== worker) | |
worker.terminate() | |
}) | |
}) | |
} | |
} | |
const pool = new WorkerPool(4, './worker.js') | |
const arr = [[1, 10],[2, 15],[3, 21],[4, 25],[5, 86]] | |
for (const ele of arr) { | |
pool.execute(ele[0], ele[1]) | |
.then((res) => console.log(res)) | |
} | |
// worker.js | |
const { parentPort } = require('worker_threads') | |
function sum(a, b) { | |
return `The sum of ${a} and ${b} is ${a+b}` | |
} | |
parentPort.on('message', ({data, port}) => { | |
const result = sum(...data) | |
port.postMessage(result) | |
port.close() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment