| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5d79373 commit 0d135e8
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,6 +61,38 @@ Worker threads inherit non-process-specific options by default. Refer to | |||
| 61 | 61 | [`Worker constructor options`][] to know how to customize worker thread options, | |
| 62 | 62 | specifically `argv` and `execArgv` options. | |
| 63 | 63 | ||
| 64 | + ## `worker.getEnvironmentData(key)` | ||
| 65 | + <!-- YAML | ||
| 66 | + added: REPLACEME | ||
| 67 | + --> | ||
| 68 | + | ||
| 69 | + > Stability: 1 - Experimental | ||
| 70 | + | ||
| 71 | + * `key` {any} Any arbitrary, cloneable JavaScript value that can be used as a | ||
| 72 | + {Map} key. | ||
| 73 | + * Returns: {any} | ||
| 74 | + | ||
| 75 | + Within a worker thread, `worker.getEnvironmentData()` returns a clone | ||
| 76 | + of data passed to the spawning thread's `worker.setEnvironmentData()`. | ||
| 77 | + Every new `Worker` receives its own copy of the environment data | ||
| 78 | + automatically. | ||
| 79 | + | ||
| 80 | + ```js | ||
| 81 | + const { | ||
| 82 | + Worker, | ||
| 83 | + isMainThread, | ||
| 84 | + setEnvironmentData, | ||
| 85 | + getEnvironmentData, | ||
| 86 | + } = require('worker_threads'); | ||
| 87 | + | ||
| 88 | + if (isMainThread) { | ||
| 89 | + setEnvironmentData('Hello', 'World!'); | ||
| 90 | + const worker = new Worker(__filename); | ||
| 91 | + } else { | ||
| 92 | + console.log(getEnvironmentData('Hello')); // Prints 'World!'. | ||
| 93 | + } | ||
| 94 | + ``` | ||
| 95 | + | ||
| 64 | 96 | ## `worker.isMainThread` | |
| 65 | 97 | <!-- YAML | |
| 66 | 98 | added: v10.5.0 | |
@@ -246,6 +278,23 @@ new Worker('process.env.SET_IN_WORKER = "foo"', { eval: true, env: SHARE_ENV }) | |||
| 246 | 278 | }); | |
| 247 | 279 | ``` | |
| 248 | 280 | ||
| 281 | + ## `worker.setEnvironmentData(key[, value])` | ||
| 282 | + <!--YAML | ||
| 283 | + added: REPLACEME | ||
| 284 | + --> | ||
| 285 | + | ||
| 286 | + > Stability: 1 - Experimental | ||
| 287 | + | ||
| 288 | + * `key` {any} Any arbitrary, cloneable JavaScript value that can be used as a | ||
| 289 | + {Map} key. | ||
| 290 | + * `value` {any} Any arbitrary, cloneable JavaScript value that will be cloned | ||
| 291 | + and passed automatically to all new `Worker` instances. If `value` is passed | ||
| 292 | + as `undefined`, any previously set value for the `key` will be deleted. | ||
| 293 | + | ||
| 294 | + The `worker.setEnvironmentData()` API sets the content of | ||
| 295 | + `worker.getEnvironmentData()` in the current thread and all new `Worker` | ||
| 296 | + instances spawned from the current context. | ||
| 297 | + | ||
| 249 | 298 | ## `worker.threadId` | |
| 250 | 299 | <!-- YAML | |
| 251 | 300 | added: v10.5.0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -105,6 +105,7 @@ port.on('message', (message) => { | |||
| 105 | 105 | filename, | |
| 106 | 106 | doEval, | |
| 107 | 107 | workerData, | |
| 108 | + environmentData, | ||
| 108 | 109 | publicPort, | |
| 109 | 110 | manifestSrc, | |
| 110 | 111 | manifestURL, | |
@@ -127,6 +128,8 @@ port.on('message', (message) => { | |||
| 127 | 128 | publicWorker.parentPort = publicPort; | |
| 128 | 129 | publicWorker.workerData = workerData; | |
| 129 | 130 | ||
| 131 | + require('internal/worker').assignEnvironmentData(environmentData); | ||
| 132 | + | ||
| 130 | 133 | // The counter is only passed to the workers created by the main thread, not | |
| 131 | 134 | // to workers created by other workers. | |
| 132 | 135 | let cachedCwd = ''; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,6 +18,7 @@ const { | |||
| 18 | 18 | ReflectApply, | |
| 19 | 19 | RegExpPrototypeTest, | |
| 20 | 20 | SafeArrayIterator, | |
| 21 | + SafeMap, | ||
| 21 | 22 | String, | |
| 22 | 23 | Symbol, | |
| 23 | 24 | SymbolFor, | |
@@ -90,6 +91,8 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => { | |||
| 90 | 91 | ||
| 91 | 92 | let cwdCounter; | |
| 92 | 93 | ||
| 94 | + const environmentData = new SafeMap(); | ||
| 95 | + | ||
| 93 | 96 | if (isMainThread) { | |
| 94 | 97 | cwdCounter = new Uint32Array(new SharedArrayBuffer(4)); | |
| 95 | 98 | const originalChdir = process.chdir; | |
@@ -99,6 +102,24 @@ if (isMainThread) { | |||
| 99 | 102 | }; | |
| 100 | 103 | } | |
| 101 | 104 | ||
| 105 | + function setEnvironmentData(key, value) { | ||
| 106 | + if (value === undefined) | ||
| 107 | + environmentData.delete(key); | ||
| 108 | + else | ||
| 109 | + environmentData.set(key, value); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + function getEnvironmentData(key) { | ||
| 113 | + return environmentData.get(key); | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + function assignEnvironmentData(data) { | ||
| 117 | + if (data === undefined) return; | ||
| 118 | + data.forEach((value, key) => { | ||
| 119 | + environmentData.set(key, value); | ||
| 120 | + }); | ||
| 121 | + } | ||
| 122 | + | ||
| 102 | 123 | class Worker extends EventEmitter { | |
| 103 | 124 | constructor(filename, options = {}) { | |
| 104 | 125 | super(); | |
@@ -228,6 +249,7 @@ class Worker extends EventEmitter { | |||
| 228 | 249 | doEval, | |
| 229 | 250 | cwdCounter: cwdCounter || workerIo.sharedCwdCounter, | |
| 230 | 251 | workerData: options.workerData, | |
| 252 | + environmentData, | ||
| 231 | 253 | publicPort: port2, | |
| 232 | 254 | manifestURL: getOptionValue('--experimental-policy') ? | |
| 233 | 255 | require('internal/process/policy').url : | |
@@ -493,6 +515,9 @@ module.exports = { | |||
| 493 | 515 | SHARE_ENV, | |
| 494 | 516 | resourceLimits: | |
| 495 | 517 | !isMainThread ? makeResourceLimits(resourceLimitsRaw) : {}, | |
| 518 | + setEnvironmentData, | ||
| 519 | + getEnvironmentData, | ||
| 520 | + assignEnvironmentData, | ||
| 496 | 521 | threadId, | |
| 497 | 522 | Worker, | |
| 498 | 523 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,8 @@ const { | |||
| 4 | 4 | isMainThread, | |
| 5 | 5 | SHARE_ENV, | |
| 6 | 6 | resourceLimits, | |
| 7 | + setEnvironmentData, | ||
| 8 | + getEnvironmentData, | ||
| 7 | 9 | threadId, | |
| 8 | 10 | Worker | |
| 9 | 11 | } = require('internal/worker'); | |
@@ -34,4 +36,6 @@ module.exports = { | |||
| 34 | 36 | parentPort: null, | |
| 35 | 37 | workerData: null, | |
| 36 | 38 | BroadcastChannel, | |
| 39 | + setEnvironmentData, | ||
| 40 | + getEnvironmentData, | ||
| 37 | 41 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + require('../common'); | ||
| 4 | + const { | ||
| 5 | + Worker, | ||
| 6 | + getEnvironmentData, | ||
| 7 | + setEnvironmentData, | ||
| 8 | + threadId, | ||
| 9 | + } = require('worker_threads'); | ||
| 10 | + | ||
| 11 | + const { | ||
| 12 | + deepStrictEqual, | ||
| 13 | + strictEqual, | ||
| 14 | + } = require('assert'); | ||
| 15 | + | ||
| 16 | + if (!process.env.HAS_STARTED_WORKER) { | ||
| 17 | + process.env.HAS_STARTED_WORKER = 1; | ||
| 18 | + setEnvironmentData('foo', 'bar'); | ||
| 19 | + setEnvironmentData('hello', { value: 'world' }); | ||
| 20 | + setEnvironmentData(1, 2); | ||
| 21 | + strictEqual(getEnvironmentData(1), 2); | ||
| 22 | + setEnvironmentData(1); // Delete it, key won't show up in the worker. | ||
| 23 | + new Worker(__filename); | ||
| 24 | + setEnvironmentData('hello'); // Delete it. Has no impact on the worker. | ||
| 25 | + } else { | ||
| 26 | + strictEqual(getEnvironmentData('foo'), 'bar'); | ||
| 27 | + deepStrictEqual(getEnvironmentData('hello'), { value: 'world' }); | ||
| 28 | + strictEqual(getEnvironmentData(1), undefined); | ||
| 29 | + | ||
| 30 | + // Recurse to make sure the environment data is inherited | ||
| 31 | + if (threadId <= 2) | ||
| 32 | + new Worker(__filename); | ||
| 33 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments