https://github.com/gera2ld/process-pool
Take advantage of multiple CPU cores in Node.js
https://github.com/gera2ld/process-pool
Last synced: about 2 months ago
JSON representation
Take advantage of multiple CPU cores in Node.js
- Host: GitHub
- URL: https://github.com/gera2ld/process-pool
- Owner: gera2ld
- Created: 2019-09-20T13:55:44.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-10-04T17:07:52.000Z (almost 7 years ago)
- Last Synced: 2026-05-12T00:32:11.695Z (2 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @gera2ld/process-pool



`process-pool` allows you to do CPU expensive stuff in multiple processes to make use of multiple CPU cores.
## Usage
Assume we have a `main.js` and `worker.js` like this:
```js
// main.js
const Pool = require('@gera2ld/process-pool');
(async () => {
const pool = new Pool(3, `${__dirname}/worker.js`);
const [result1, result2] = await Promise.all([
pool.invoke('add', [1, 2]),
pool.invoke('minus', [4, 3]),
]);
console.log(result1, result2);
await pool.destroy();
})();
```
```js
// worker.js
const worker = require('@gera2ld/process-pool/lib/worker');
class Handler {
add(a, b) {
return a + b;
}
}
// Set a handler with preset methods
worker.setHandler(new Handler());
// Add other methods
worker.setMethod('minus', (a, b) => a - b);
```