| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fe12cc0 commit 9f6bc58
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 | |
@@ -240,6 +272,23 @@ new Worker('process.env.SET_IN_WORKER = "foo"', { eval: true, env: SHARE_ENV }) | |||
| 240 | 272 | }); | |
| 241 | 273 | ``` | |
| 242 | 274 | ||
| 275 | + ## `worker.setEnvironmentData(key[, value])` | ||
| 276 | + <!--YAML | ||
| 277 | + added: REPLACEME | ||
| 278 | + --> | ||
| 279 | + | ||
| 280 | + > Stability: 1 - Experimental | ||
| 281 | + | ||
| 282 | + * `key` {any} Any arbitrary, cloneable JavaScript value that can be used as a | ||
| 283 | + {Map} key. | ||
| 284 | + * `value` {any} Any arbitrary, cloneable JavaScript value that will be cloned | ||
| 285 | + and passed automatically to all new `Worker` instances. If `value` is passed | ||
| 286 | + as `undefined`, any previously set value for the `key` will be deleted. | ||
| 287 | + | ||
| 288 | + The `worker.setEnvironmentData()` API sets the content of | ||
| 289 | + `worker.getEnvironmentData()` in the current thread and all new `Worker` | ||
| 290 | + instances spawned from the current context. | ||
| 291 | + | ||
| 243 | 292 | ## `worker.threadId` | |
| 244 | 293 | <!-- YAML | |
| 245 | 294 | added: v10.5.0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -107,6 +107,7 @@ port.on('message', (message) => { | |||
| 107 | 107 | filename, | |
| 108 | 108 | doEval, | |
| 109 | 109 | workerData, | |
| 110 | + environmentData, | ||
| 110 | 111 | publicPort, | |
| 111 | 112 | manifestSrc, | |
| 112 | 113 | manifestURL, | |
@@ -130,6 +131,8 @@ port.on('message', (message) => { | |||
| 130 | 131 | publicWorker.parentPort = publicPort; | |
| 131 | 132 | publicWorker.workerData = workerData; | |
| 132 | 133 | ||
| 134 | + require('internal/worker').assignEnvironmentData(environmentData); | ||
| 135 | + | ||
| 133 | 136 | // The counter is only passed to the workers created by the main thread, not | |
| 134 | 137 | // to workers created by other workers. | |
| 135 | 138 | let cachedCwd = ''; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,7 @@ const { | |||
| 13 | 13 | Promise, | |
| 14 | 14 | PromiseResolve, | |
| 15 | 15 | RegExpPrototypeTest, | |
| 16 | + SafeMap, | ||
| 16 | 17 | String, | |
| 17 | 18 | Symbol, | |
| 18 | 19 | SymbolFor, | |
@@ -85,6 +86,8 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => { | |||
| 85 | 86 | ||
| 86 | 87 | let cwdCounter; | |
| 87 | 88 | ||
| 89 | + const environmentData = new SafeMap(); | ||
| 90 | + | ||
| 88 | 91 | if (isMainThread) { | |
| 89 | 92 | cwdCounter = new Uint32Array(new SharedArrayBuffer(4)); | |
| 90 | 93 | const originalChdir = process.chdir; | |
@@ -94,6 +97,24 @@ if (isMainThread) { | |||
| 94 | 97 | }; | |
| 95 | 98 | } | |
| 96 | 99 | ||
| 100 | + function setEnvironmentData(key, value) { | ||
| 101 | + if (value === undefined) | ||
| 102 | + environmentData.delete(key); | ||
| 103 | + else | ||
| 104 | + environmentData.set(key, value); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + function getEnvironmentData(key) { | ||
| 108 | + return environmentData.get(key); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + function assignEnvironmentData(data) { | ||
| 112 | + if (data === undefined) return; | ||
| 113 | + data.forEach((value, key) => { | ||
| 114 | + environmentData.set(key, value); | ||
| 115 | + }); | ||
| 116 | + } | ||
| 117 | + | ||
| 97 | 118 | class Worker extends EventEmitter { | |
| 98 | 119 | constructor(filename, options = {}) { | |
| 99 | 120 | super(); | |
@@ -222,6 +243,7 @@ class Worker extends EventEmitter { | |||
| 222 | 243 | doEval, | |
| 223 | 244 | cwdCounter: cwdCounter || workerIo.sharedCwdCounter, | |
| 224 | 245 | workerData: options.workerData, | |
| 246 | + environmentData, | ||
| 225 | 247 | publicPort: port2, | |
| 226 | 248 | manifestURL: getOptionValue('--experimental-policy') ? | |
| 227 | 249 | require('internal/process/policy').url : | |
@@ -482,6 +504,9 @@ module.exports = { | |||
| 482 | 504 | SHARE_ENV, | |
| 483 | 505 | resourceLimits: | |
| 484 | 506 | !isMainThread ? makeResourceLimits(resourceLimitsRaw) : {}, | |
| 507 | + setEnvironmentData, | ||
| 508 | + getEnvironmentData, | ||
| 509 | + assignEnvironmentData, | ||
| 485 | 510 | threadId, | |
| 486 | 511 | Worker, | |
| 487 | 512 | }; | |
| 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'); | |
@@ -32,4 +34,6 @@ module.exports = { | |||
| 32 | 34 | Worker, | |
| 33 | 35 | parentPort: null, | |
| 34 | 36 | workerData: null, | |
| 37 | + setEnvironmentData, | ||
| 38 | + getEnvironmentData, | ||
| 35 | 39 | }; | |
| 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