Skip to content

Commit cf1bcb3

Browse files
committed
Add worker thread pool example
1 parent 4886f6d commit cf1bcb3

6 files changed

Lines changed: 52 additions & 2 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"extends": ["eslint:recommended", "plugin:node/recommended"],
44
"rules": {
55
"node/exports-style": ["error", "module.exports"],
6-
"no-console": ["warning"]
6+
"no-console": 0
77
},
88
"env": {
99
"jest": true

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"lightship": "^4.3.0",
2929
"mongoose": "^4.13.19",
3030
"request": "^2.88.0",
31-
"request-promise": "^4.2.4"
31+
"request-promise": "^4.2.4",
32+
"worker-threads-pool": "^2.0.0"
3233
},
3334
"devDependencies": {
3435
"dtrace-provider": "^0.8.7",

src/routes/threads.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express');
22
const fb = require('fibonacci');
33
const runFibonacci = require('../workers/fibonacciWorker');
4+
const runFibonacciPool = require('../workers/fibonacciWorkerPool');
45
const log = require('../log');
56

67
const router = express.Router();
@@ -15,4 +16,9 @@ router.get('/fibonacciThreaded', async (req, res) => {
1516
res.send('processing');
1617
});
1718

19+
router.get('/fibonacciThreadedPool', async (req, res) => {
20+
runFibonacciPool({ iterations: 10000 }).then(result => log.info(result));
21+
res.send('processing');
22+
});
23+
1824
module.exports = router;

src/workers/fibonacciWorkerPool.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const fb = require('fibonacci');
2+
const { isMainThread, parentPort, workerData } = require('worker_threads');
3+
const Pool = require('worker-threads-pool');
4+
const CPUs = require('os').cpus().length;
5+
const pool = new Pool({ max: CPUs });
6+
7+
const runFibonacci = workerData => {
8+
return new Promise((resolve, reject) => {
9+
pool.acquire(__filename, { workerData }, (err, worker) => {
10+
if (err) reject(err);
11+
console.log(`started worker ${worker} (pool size: ${pool.size})`);
12+
worker.on('message', resolve);
13+
worker.on('error', reject);
14+
worker.on('exit', code => {
15+
if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`));
16+
});
17+
});
18+
});
19+
};
20+
21+
if (!isMainThread) {
22+
const number = fb.iterate(workerData.iterations);
23+
parentPort.postMessage({ number });
24+
}
25+
26+
module.exports = runFibonacci;

0 commit comments

Comments
 (0)