-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
worker: initial implementation #20876
Changes from all commits
f0ded20
2bb055b
e7695cc
6d4931a
315efb5
f2e297b
ef24c5c
a57c54f
ca0edc1
d2d9061
0c7012e
396d785
65d4542
d0535eb
17e01a2
53b660b
004075b
d44594d
fa53bd5
79970f3
168d8a6
a12f76f
ab02dbc
2947dea
735227e
cbbddea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
const { parentPort } = require('worker_threads'); | ||
|
||
parentPort.on('message', (msg) => { | ||
parentPort.postMessage(msg); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const path = require('path'); | ||
const bench = common.createBenchmark(main, { | ||
workers: [1], | ||
payload: ['string', 'object'], | ||
sendsPerBroadcast: [1, 10], | ||
n: [1e5] | ||
}, { flags: ['--experimental-worker'] }); | ||
|
||
const workerPath = path.resolve(__dirname, '..', 'fixtures', 'echo.worker.js'); | ||
|
||
function main(conf) { | ||
const { Worker } = require('worker_threads'); | ||
|
||
const n = +conf.n; | ||
const workers = +conf.workers; | ||
const sends = +conf.sendsPerBroadcast; | ||
const expectedPerBroadcast = sends * workers; | ||
var payload; | ||
var readies = 0; | ||
var broadcasts = 0; | ||
var msgCount = 0; | ||
|
||
switch (conf.payload) { | ||
case 'string': | ||
payload = 'hello world!'; | ||
break; | ||
case 'object': | ||
payload = { action: 'pewpewpew', powerLevel: 9001 }; | ||
break; | ||
default: | ||
throw new Error('Unsupported payload type'); | ||
} | ||
|
||
const workerObjs = []; | ||
|
||
for (var i = 0; i < workers; ++i) { | ||
const worker = new Worker(workerPath); | ||
workerObjs.push(worker); | ||
worker.on('online', onOnline); | ||
worker.on('message', onMessage); | ||
} | ||
|
||
function onOnline() { | ||
if (++readies === workers) { | ||
bench.start(); | ||
broadcast(); | ||
} | ||
} | ||
|
||
function broadcast() { | ||
if (broadcasts++ === n) { | ||
bench.end(n); | ||
for (const worker of workerObjs) { | ||
worker.unref(); | ||
} | ||
return; | ||
} | ||
for (const worker of workerObjs) { | ||
for (var i = 0; i < sends; ++i) | ||
worker.postMessage(payload); | ||
} | ||
} | ||
|
||
function onMessage() { | ||
if (++msgCount === expectedPerBroadcast) { | ||
msgCount = 0; | ||
broadcast(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,5 @@ | |
@include util | ||
@include v8 | ||
@include vm | ||
@include worker_threads | ||
@include zlib |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -410,6 +410,8 @@ added: v0.7.0 | |
The `process.abort()` method causes the Node.js process to exit immediately and | ||
generate a core file. | ||
|
||
This feature is not available in [`Worker`][] threads. | ||
|
||
## process.arch | ||
<!-- YAML | ||
added: v0.5.0 | ||
|
@@ -517,6 +519,8 @@ try { | |
} | ||
``` | ||
|
||
This feature is not available in [`Worker`][] threads. | ||
|
||
## process.config | ||
<!-- YAML | ||
added: v0.7.7 | ||
|
@@ -918,6 +922,8 @@ console.log(process.env.test); | |
// => 1 | ||
``` | ||
|
||
`process.env` is read-only in [`Worker`][] threads. | ||
|
||
## process.execArgv | ||
<!-- YAML | ||
added: v0.7.7 | ||
|
@@ -1030,6 +1036,9 @@ If it is necessary to terminate the Node.js process due to an error condition, | |
throwing an *uncaught* error and allowing the process to terminate accordingly | ||
is safer than calling `process.exit()`. | ||
|
||
In [`Worker`][] threads, this function stops the current thread rather | ||
than the current process. | ||
|
||
## process.exitCode | ||
<!-- YAML | ||
added: v0.11.8 | ||
|
@@ -1203,6 +1212,7 @@ console.log(process.getgroups()); // [ 27, 30, 46, 1000 ] | |
|
||
This function is only available on POSIX platforms (i.e. not Windows or | ||
Android). | ||
This feature is not available in [`Worker`][] threads. | ||
|
||
## process.kill(pid[, signal]) | ||
<!-- YAML | ||
|
@@ -1306,6 +1316,9 @@ The _heap_ is where objects, strings, and closures are stored. Variables are | |
stored in the _stack_ and the actual JavaScript code resides in the | ||
_code segment_. | ||
|
||
When using [`Worker`][] threads, `rss` will be a value that is valid for the | ||
entire process, while the other fields will only refer to the current thread. | ||
|
||
## process.nextTick(callback[, ...args]) | ||
<!-- YAML | ||
added: v0.1.26 | ||
|
@@ -1569,6 +1582,7 @@ if (process.getegid && process.setegid) { | |
|
||
This function is only available on POSIX platforms (i.e. not Windows or | ||
Android). | ||
This feature is not available in [`Worker`][] threads. | ||
|
||
## process.seteuid(id) | ||
<!-- YAML | ||
|
@@ -1596,6 +1610,7 @@ if (process.geteuid && process.seteuid) { | |
|
||
This function is only available on POSIX platforms (i.e. not Windows or | ||
Android). | ||
This feature is not available in [`Worker`][] threads. | ||
|
||
## process.setgid(id) | ||
<!-- YAML | ||
|
@@ -1623,6 +1638,7 @@ if (process.getgid && process.setgid) { | |
|
||
This function is only available on POSIX platforms (i.e. not Windows or | ||
Android). | ||
This feature is not available in [`Worker`][] threads. | ||
|
||
## process.setgroups(groups) | ||
<!-- YAML | ||
|
@@ -1639,6 +1655,7 @@ The `groups` array can contain numeric group IDs, group names or both. | |
|
||
This function is only available on POSIX platforms (i.e. not Windows or | ||
Android). | ||
This feature is not available in [`Worker`][] threads. | ||
|
||
## process.setuid(id) | ||
<!-- YAML | ||
|
@@ -1664,6 +1681,7 @@ if (process.getuid && process.setuid) { | |
|
||
This function is only available on POSIX platforms (i.e. not Windows or | ||
Android). | ||
This feature is not available in [`Worker`][] threads. | ||
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this was intended by me, because the sentence before also explains when this feature is not available → it made sense to me to have them be a single paragraph
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vsemozhetbyt Yup, done! (Feel free to push documentation change commits to this branch, btw.) |
||
|
||
## process.setUncaughtExceptionCaptureCallback(fn) | ||
<!-- YAML | ||
|
@@ -1700,6 +1718,8 @@ a [Writable][] stream. | |
`process.stderr` differs from other Node.js streams in important ways, see | ||
[note on process I/O][] for more information. | ||
|
||
This feature is not available in [`Worker`][] threads. | ||
|
||
## process.stdin | ||
|
||
* {Stream} | ||
|
@@ -1732,6 +1752,8 @@ In "old" streams mode the `stdin` stream is paused by default, so one | |
must call `process.stdin.resume()` to read from it. Note also that calling | ||
`process.stdin.resume()` itself would switch stream to "old" mode. | ||
|
||
This feature is not available in [`Worker`][] threads. | ||
|
||
## process.stdout | ||
|
||
* {Stream} | ||
|
@@ -1750,6 +1772,8 @@ process.stdin.pipe(process.stdout); | |
`process.stdout` differs from other Node.js streams in important ways, see | ||
[note on process I/O][] for more information. | ||
|
||
This feature is not available in [`Worker`][] threads. | ||
|
||
### A note on process I/O | ||
|
||
`process.stdout` and `process.stderr` differ from other Node.js streams in | ||
|
@@ -1865,6 +1889,8 @@ console.log( | |
); | ||
``` | ||
|
||
This feature is not available in [`Worker`][] threads. | ||
|
||
## process.uptime() | ||
<!-- YAML | ||
added: v0.5.0 | ||
|
@@ -1992,6 +2018,7 @@ cases: | |
[`ChildProcess`]: child_process.html#child_process_class_childprocess | ||
[`Error`]: errors.html#errors_class_error | ||
[`EventEmitter`]: events.html#events_class_eventemitter | ||
[`Worker`]: worker_threads.html#worker_threads_class_worker | ||
[`console.error()`]: console.html#console_console_error_data_args | ||
[`console.log()`]: console.html#console_console_log_data_args | ||
[`domain`]: domain.html | ||
|
This comment was marked as resolved.
Sorry, something went wrong.