| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6caa354 commit 03fbc9e
33 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - const { parentPort } = require('worker'); | ||
| 3 | + const { parentPort } = require('worker_threads'); | ||
| 4 | 4 | ||
| 5 | 5 | parentPort.on('message', (msg) => { | |
| 6 | 6 | parentPort.postMessage(msg); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ const bench = common.createBenchmark(main, { | |||
| 12 | 12 | const workerPath = path.resolve(__dirname, '..', 'fixtures', 'echo.worker.js'); | |
| 13 | 13 | ||
| 14 | 14 | function main(conf) { | |
| 15 | - const { Worker } = require('worker'); | ||
| 15 | + const { Worker } = require('worker_threads'); | ||
| 16 | 16 | ||
| 17 | 17 | const n = +conf.n; | |
| 18 | 18 | const workers = +conf.workers; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,7 +53,7 @@ | |||
| 53 | 53 | * [Utilities](util.html) | |
| 54 | 54 | * [V8](v8.html) | |
| 55 | 55 | * [VM](vm.html) | |
| 56 | - * [Worker](worker.html) | ||
| 56 | + * [Worker Threads](worker_threads.html) | ||
| 57 | 57 | * [ZLIB](zlib.html) | |
| 58 | 58 | ||
| 59 | 59 | <div class="line"></div> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,5 +46,5 @@ | |||
| 46 | 46 | @include util | |
| 47 | 47 | @include v8 | |
| 48 | 48 | @include vm | |
| 49 | - @include worker | ||
| 49 | + @include worker_threads | ||
| 50 | 50 | @include zlib | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -728,4 +728,4 @@ never be called. | |||
| 728 | 728 | [Hook Callbacks]: #async_hooks_hook_callbacks | |
| 729 | 729 | [PromiseHooks]: https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk | |
| 730 | 730 | [promise execution tracking]: #async_hooks_promise_execution_tracking | |
| 731 | - [`Worker`]: worker.html#worker_worker | ||
| 731 | + [`Worker`]: worker_threads.html#worker_threads_class_worker | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2018,7 +2018,7 @@ cases: | |||
| 2018 | 2018 | [`ChildProcess`]: child_process.html#child_process_class_childprocess | |
| 2019 | 2019 | [`Error`]: errors.html#errors_class_error | |
| 2020 | 2020 | [`EventEmitter`]: events.html#events_class_eventemitter | |
| 2021 | - [`Worker`]: worker.html#worker_worker | ||
| 2021 | + [`Worker`]: worker_threads.html#worker_threads_class_worker | ||
| 2022 | 2022 | [`console.error()`]: console.html#console_console_error_data_args | |
| 2023 | 2023 | [`console.log()`]: console.html#console_console_log_data_args | |
| 2024 | 2024 | [`domain`]: domain.html | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - # Worker | ||
| 1 | + # Worker Threads | ||
| 2 | 2 | ||
| 3 | 3 | <!--introduced_in=REPLACEME--> | |
| 4 | 4 | ||
@@ -9,7 +9,7 @@ on independent threads, and to create message channels between them. It | |||
| 9 | 9 | can be accessed using: | |
| 10 | 10 | ||
| 11 | 11 | ```js | |
| 12 | - const worker = require('worker'); | ||
| 12 | + const worker = require('worker_threads'); | ||
| 13 | 13 | ``` | |
| 14 | 14 | ||
| 15 | 15 | Workers are useful for performing CPU-intensive JavaScript operations; do not | |
@@ -23,7 +23,9 @@ share memory efficiently by transferring `ArrayBuffer` instances or sharing | |||
| 23 | 23 | ## Example | |
| 24 | 24 | ||
| 25 | 25 | ```js | |
| 26 | - const { Worker, isMainThread, parentPort, workerData } = require('worker'); | ||
| 26 | + const { | ||
| 27 | + Worker, isMainThread, parentPort, workerData | ||
| 28 | + } = require('worker_threads'); | ||
| 27 | 29 | ||
| 28 | 30 | if (isMainThread) { | |
| 29 | 31 | module.exports = async function parseJSAsync(script) { | |
@@ -104,7 +106,7 @@ yields an object with `port1` and `port2` properties, which refer to linked | |||
| 104 | 106 | [`MessagePort`][] instances. | |
| 105 | 107 | ||
| 106 | 108 | ```js | |
| 107 | - const { MessageChannel } = require('worker'); | ||
| 109 | + const { MessageChannel } = require('worker_threads'); | ||
| 108 | 110 | ||
| 109 | 111 | const { port1, port2 } = new MessageChannel(); | |
| 110 | 112 | port1.on('message', (message) => console.log('received', message)); | |
@@ -241,8 +243,8 @@ Notable differences inside a Worker environment are: | |||
| 241 | 243 | ||
| 242 | 244 | - The [`process.stdin`][], [`process.stdout`][] and [`process.stderr`][] | |
| 243 | 245 | may be redirected by the parent thread. | |
| 244 | - - The [`require('worker').isMainThread`][] property is set to `false`. | ||
| 245 | - - The [`require('worker').parentPort`][] message port is available, | ||
| 246 | + - The [`require('worker_threads').isMainThread`][] property is set to `false`. | ||
| 247 | + - The [`require('worker_threads').parentPort`][] message port is available, | ||
| 246 | 248 | - [`process.exit()`][] does not stop the whole program, just the single thread, | |
| 247 | 249 | and [`process.abort()`][] is not available. | |
| 248 | 250 | - [`process.chdir()`][] and `process` methods that set group or user ids | |
@@ -283,7 +285,9 @@ For example: | |||
| 283 | 285 | ||
| 284 | 286 | ```js | |
| 285 | 287 | const assert = require('assert'); | |
| 286 | - const { Worker, MessageChannel, MessagePort, isMainThread } = require('worker'); | ||
| 288 | + const { | ||
| 289 | + Worker, MessageChannel, MessagePort, isMainThread | ||
| 290 | + } = require('worker_threads'); | ||
| 287 | 291 | if (isMainThread) { | |
| 288 | 292 | const worker = new Worker(__filename); | |
| 289 | 293 | const subChannel = new MessageChannel(); | |
@@ -292,7 +296,7 @@ if (isMainThread) { | |||
| 292 | 296 | console.log('received:', value); | |
| 293 | 297 | }); | |
| 294 | 298 | } else { | |
| 295 | - require('worker').once('workerMessage', (value) => { | ||
| 299 | + require('worker_threads').once('workerMessage', (value) => { | ||
| 296 | 300 | assert(value.hereIsYourPort instanceof MessagePort); | |
| 297 | 301 | value.hereIsYourPort.postMessage('the worker is sending this'); | |
| 298 | 302 | value.hereIsYourPort.close(); | |
@@ -309,9 +313,9 @@ if (isMainThread) { | |||
| 309 | 313 | * `eval` {boolean} If true, interpret the first argument to the constructor | |
| 310 | 314 | as a script that is executed once the worker is online. | |
| 311 | 315 | * `data` {any} Any JavaScript value that will be cloned and made | |
| 312 | - available as [`require('worker').workerData`][]. The cloning will occur as | ||
| 313 | - described in the [HTML structured clone algorithm][], and an error will be | ||
| 314 | - thrown if the object cannot be cloned (e.g. because it contains | ||
| 316 | + available as [`require('worker_threads').workerData`][]. The cloning will | ||
| 317 | + occur as described in the [HTML structured clone algorithm][], and an error | ||
| 318 | + will be thrown if the object cannot be cloned (e.g. because it contains | ||
| 315 | 319 | `function`s). | |
| 316 | 320 | * stdin {boolean} If this is set to `true`, then `worker.stdin` will | |
| 317 | 321 | provide a writable stream whose contents will appear as `process.stdin` | |
@@ -351,8 +355,8 @@ added: REPLACEME | |||
| 351 | 355 | * `value` {any} The transmitted value | |
| 352 | 356 | ||
| 353 | 357 | The `'message'` event is emitted when the worker thread has invoked | |
| 354 | - [`require('worker').postMessage()`][]. See the [`port.on('message')`][] event | ||
| 355 | - for more details. | ||
| 358 | + [`require('worker_threads').postMessage()`][]. See the [`port.on('message')`][] | ||
| 359 | + event for more details. | ||
| 356 | 360 | ||
| 357 | 361 | ### Event: 'online' | |
| 358 | 362 | <!-- YAML | |
@@ -371,8 +375,8 @@ added: REPLACEME | |||
| 371 | 375 | * `transferList` {Object[]} | |
| 372 | 376 | ||
| 373 | 377 | Send a message to the worker that will be received via | |
| 374 | - [`require('worker').on('workerMessage')`][]. See [`port.postMessage()`][] for | ||
| 375 | - more details. | ||
| 378 | + [`require('worker_threads').on('workerMessage')`][]. | ||
| 379 | + See [`port.postMessage()`][] for more details. | ||
| 376 | 380 | ||
| 377 | 381 | ### worker.ref() | |
| 378 | 382 | <!-- YAML | |
@@ -444,7 +448,7 @@ added: REPLACEME | |||
| 444 | 448 | * {integer} | |
| 445 | 449 | ||
| 446 | 450 | An integer identifier for the referenced thread. Inside the worker thread, | |
| 447 | - it is available as [`require('worker').threadId`][]. | ||
| 451 | + it is available as [`require('worker_threads').threadId`][]. | ||
| 448 | 452 | ||
| 449 | 453 | ### worker.unref() | |
| 450 | 454 | <!-- YAML | |
@@ -457,14 +461,14 @@ active handle in the event system. If the worker is already `unref()`ed calling | |||
| 457 | 461 | ||
| 458 | 462 | [`Buffer`]: buffer.html | |
| 459 | 463 | [`EventEmitter`]: events.html | |
| 460 | - [`MessagePort`]: #worker_class_messageport | ||
| 461 | - [`port.postMessage()`]: #worker_port_postmessage_value_transferlist | ||
| 462 | - [`Worker`]: #worker_class_worker | ||
| 463 | - [`worker.terminate()`]: #worker_worker_terminate_callback | ||
| 464 | - [`worker.postMessage()`]: #worker_worker_postmessage_value_transferlist_1 | ||
| 465 | - [`worker.on('message')`]: #worker_event_message_1 | ||
| 466 | - [`worker.threadId`]: #worker_worker_threadid_1 | ||
| 467 | - [`port.on('message')`]: #worker_event_message | ||
| 464 | + [`MessagePort`]: #worker_threads_class_messageport | ||
| 465 | + [`port.postMessage()`]: #worker_threads_port_postmessage_value_transferlist | ||
| 466 | + [`Worker`]: #worker_threads_class_worker | ||
| 467 | + [`worker.terminate()`]: #worker_threads_worker_terminate_callback | ||
| 468 | + [`worker.postMessage()`]: #worker_threads_worker_postmessage_value_transferlist_1 | ||
| 469 | + [`worker.on('message')`]: #worker_threads_event_message_1 | ||
| 470 | + [`worker.threadId`]: #worker_threads_worker_threadid_1 | ||
| 471 | + [`port.on('message')`]: #worker_threads_event_message | ||
| 468 | 472 | [`process.exit()`]: process.html#process_process_exit_code | |
| 469 | 473 | [`process.abort()`]: process.html#process_process_abort | |
| 470 | 474 | [`process.chdir()`]: process.html#process_process_chdir_directory | |
@@ -473,11 +477,11 @@ active handle in the event system. If the worker is already `unref()`ed calling | |||
| 473 | 477 | [`process.stderr`]: process.html#process_process_stderr | |
| 474 | 478 | [`process.stdout`]: process.html#process_process_stdout | |
| 475 | 479 | [`process.title`]: process.html#process_process_title | |
| 476 | - [`require('worker').workerData`]: #worker_worker_workerdata | ||
| 477 | - [`require('worker').on('workerMessage')`]: #worker_event_workermessage | ||
| 478 | - [`require('worker').postMessage()`]: #worker_worker_postmessage_value_transferlist | ||
| 479 | - [`require('worker').isMainThread`]: #worker_worker_ismainthread | ||
| 480 | - [`require('worker').threadId`]: #worker_worker_threadid | ||
| 480 | + [`require('worker_threads').workerData`]: #worker_threads_worker_workerdata | ||
| 481 | + [`require('worker_threads').on('workerMessage')`]: #worker_threads_event_workermessage | ||
| 482 | + [`require('worker_threads').postMessage()`]: #worker_threads_worker_postmessage_value_transferlist | ||
| 483 | + [`require('worker_threads').isMainThread`]: #worker_threads_worker_ismainthread | ||
| 484 | + [`require('worker_threads').threadId`]: #worker_threads_worker_threadid | ||
| 481 | 485 | [`cluster` module]: cluster.html | |
| 482 | 486 | [`inspector`]: inspector.html | |
| 483 | 487 | [v8.serdes]: v8.html#v8_serialization_api | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -195,7 +195,8 @@ | |||
| 195 | 195 | ||
| 196 | 196 | NativeModule.isInternal = function(id) { | |
| 197 | 197 | return id.startsWith('internal/') || | |
| 198 | - (id === 'worker' && !process.binding('config').experimentalWorker); | ||
| 198 | + (id === 'worker_threads' && | ||
| 199 | + !process.binding('config').experimentalWorker); | ||
| 199 | 200 | }; | |
| 200 | 201 | } | |
| 201 | 202 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -106,7 +106,7 @@ const builtinLibs = [ | |||
| 106 | 106 | ]; | |
| 107 | 107 | ||
| 108 | 108 | if (process.binding('config').experimentalWorker) { | |
| 109 | - builtinLibs.push('worker'); | ||
| 109 | + builtinLibs.push('worker_threads'); | ||
| 110 | 110 | builtinLibs.sort(); | |
| 111 | 111 | } | |
| 112 | 112 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -387,7 +387,7 @@ function setupChild(evalScript) { | |||
| 387 | 387 | debug(`[${threadId}] is setting up worker child environment`); | |
| 388 | 388 | const port = getEnvMessagePort(); | |
| 389 | 389 | ||
| 390 | - const publicWorker = require('worker'); | ||
| 390 | + const publicWorker = require('worker_threads'); | ||
| 391 | 391 | ||
| 392 | 392 | port.on('message', (message) => { | |
| 393 | 393 | if (message.type === 'loadScript') { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments