|
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 |
|