Skip to content

Commit dda8c0a

Browse files
Krzysztof Słonkaslonka
Krzysztof Słonka
authored andcommitted
use experimental worker_threads to run tasks
1 parent eb32cff commit dda8c0a

File tree

13 files changed

+1439
-171
lines changed

13 files changed

+1439
-171
lines changed

Readme.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55
# worker-nodes
66

7+
*THIS IS AN EXPERIMENTAL BRANCH, IT USES [worker_threads](https://nodejs.org/api/worker_threads.html) WHICH IS NOT STABLE YET*
8+
79
A node.js library to run cpu-intensive tasks in a separate processes and to not block the event loop.
810

911

1012
## Installation
1113

1214
```bash
13-
$ npm install worker-nodes
15+
$ npm install worker-nodes@next
1416
```
1517

16-
Node.js greater than 6.6.0 highly recommended.
18+
Node.js greater than 10.5.0 is *required*
1719

1820
# API Reference
1921

@@ -206,18 +208,18 @@ Example results:
206208
```bash
207209
results for 100 executions
208210

209-
name time: total [ms] time usr [ms] time sys [ms] worker usr [ms] worker sys [ms] mem rss [MB] worker rss [MB] errors
210-
------------------ ---------------- ------------- ------------- --------------- --------------- ------------ --------------- ------
211-
no-workers 150 239 42 0 0 98 0 0
212-
worker-nodes@1.2.0 1521 646 528 641 367 272 119 0
213-
[email protected] 12055 7356 5726 896 212 731 74 0
214-
worker-farm@1.3.1 12124 6711 5501 1577 446 689 74 0
215-
[email protected] 12348 6866 5474 1696 458 698 76 0
216-
worker-pool@3.0.2 14029 7633 5604 2285 649 769 104 0
217-
218-
os : Darwin / 15.5.0 / x64
219-
cpu : Intel(R) Core(TM) i7-4578U CPU @ 3.00GHz × 4
220-
node : 6.9.1 / v8: 5.1.281.84
211+
name time: total [ms] time usr [ms] time sys [ms] worker usr [ms] worker sys [ms] mem rss [MB] worker rss [MB] errors
212+
------------------ ---------------- ------------- ------------- --------------- --------------- ------------ --------------- ------
213+
no-workers 162 250 38 0 0 101 0 0
214+
worker-nodes@next 458 573 186 571 185 210 207 0
215+
[email protected] 1503 670 395 991 337 292 87 0
216+
workerpool@2.3.0 1684 1511 688 292 82 155 49 0
217+
[email protected] 2508 1435 511 1247 368 104 59 0
218+
process-pool@0.3.4 2571 1537 517 1333 376 105 61 0
219+
[email protected] 15939 15984 5632 1946 546 86 79 0
220+
221+
os : Darwin / 17.5.0 / x64
222+
cpu : Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz × 8
221223
```
222224

223225
## See also

benchmark/package-lock.json

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

benchmark/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
},
1515
"scripts": {
1616
"pretest": "mkdir -p fixtures; cd fixtures; base64 /dev/urandom | head -c 1048576 > output.dat; base64 /dev/urandom | head -c 524288 > input.dat;",
17-
"test": "node reporter.js --repeats 100 ./html-renderer/*"
17+
"test": "node --experimental-worker reporter.js --repeats 100 ./html-renderer/*"
1818
}
1919
}

lib/util/queue.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class Queue {
3838
return this.storage.forEach(predicate);
3939
}
4040

41+
map(predicate) {
42+
return this.storage.map(predicate);
43+
}
44+
4145
filter(predicate) {
4246
return this.storage.filter(predicate);
4347
}

lib/worker/child-loader.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
const net = require('net');
1+
const { parentPort } = require('worker_threads');
22

33
const { Request, Response } = require('./message');
4-
const Transport = require('./transport');
54

65
let $module;
7-
let $cmdTransport = process;
8-
let $dataTransport;
96

107
function setupModule({ modulePath }) {
118
// load target module
129
$module = require(modulePath);
1310

14-
// setup data transport channel
15-
$dataTransport = new Transport(new net.Socket({ fd: 3 }));
16-
$dataTransport.on('message', handleCall);
11+
// setup data channel
12+
parentPort.on('message', handleCall);
1713

1814
// report readiness
19-
$cmdTransport.send('ready');
15+
parentPort.postMessage('ready');
2016
}
2117

2218
function handleCall(requestData) {
@@ -33,7 +29,7 @@ function handleCall(requestData) {
3329
})
3430
.then(result => {
3531
response.setResult(result);
36-
$dataTransport.send(response);
32+
parentPort.postMessage(response);
3733
})
3834
.catch(err => {
3935
const error = {
@@ -45,15 +41,15 @@ function handleCall(requestData) {
4541
Object.keys(err).forEach(key => error[key] = err[key]);
4642
response.error = error;
4743

48-
$dataTransport.send(response);
44+
parentPort.postMessage(response);
4945
});
5046
}
5147

52-
$cmdTransport.on('message', function ({ cmd = 'call', data }) {
48+
parentPort.on('message', function ({ cmd = 'call', data }) {
5349
switch (cmd) {
5450
case 'start':
5551
return setupModule(data);
5652
case 'exit':
5753
return process.exit(0);
5854
}
59-
});
55+
});

0 commit comments

Comments
 (0)