全局变量赋值在NodeJS上是原子的吗? [英] Is global variable assignment atomic on NodeJS?

查看:136
本文介绍了全局变量赋值在NodeJS上是原子的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Node上的有状态websocket服务器上工作.我们需要一条对于biz逻辑很重要的只读数据,并将其保存在Node的全局范围内,最近,我们不得不使此要求缓存无效,并重新要求允许在运行时更改此数据.我的问题是,我们是否应该担心仅仅重新分配全局变量会导致重新请求的结果会导致并发连接之间出现问题,这些并发连接在不同阶段读取此单个数据副本?谢谢!

I’m working on a stateful and websocket server on Node. We require a piece of read-only data that is important for the biz logic and save it in Node’s global scope, and recently we have to invalidate this require cache and re-require to allow changes of this piece of data at runtime. My question is should we worry simply reassigning the global variable the result of the re-require would cause issues between concurrent connections which read this single copy of data at different phases? Thanks!

推荐答案

NodeJS上的全局变量赋值是原子的吗?

Is global variable assignment atomic on NodeJS?

由于node.js仅将Javascript作为单线程运行,因此所有分配都是Javascript中的原子操作.

Because node.js runs your Javascript as single threaded only, all assignments are atomic in Javascript.

我们应该担心只是重新分配全局变量会导致重新请求的结果会导致并发连接之间出现问题,这些并发连接在不同阶段读取此数据的单个副本吗?

Should we worry simply reassigning the global variable the result of the re-require would cause issues between concurrent connections which read this single copy of data at different phases?

您不必担心分配不是原子的.

You don't have to worry about an assignment not being atomic.

如果一行代码读取此全局变量并将其值分配给另一个局部变量,然后再将其重新分配给全局变量,那么该局部变量的值在重新分配后是否会自动刷新?

If a line of code reads this global variable and assigns its value to another local variable before the reassignment to the global var, would the value of this local variable be automatically refreshed after the reassignment?

先前访问数据的模块在分配后是否会看到新数据取决于代码的确切细节以及变量中的内容.要具体说明您的情况,您必须向我们显示实际代码以及该操作中分配的变量中的内容.

Whether the module that previously had access to the data will see the new data after assignment or not depends upon the precise details of the code and what's in the variables. To comment specifically on your situation, you'd have to show us the actual code and what is in the variables that are assigned in the operation.

这是Javascript中分配的一般说明.赋值会根据变量中的内容执行不同的操作.

Here's a general description of assignment in Javascript. Assignment does something different based on what's in the variable.

原语在分配时被复制.通过共享对象的指针(不复制)来分配对象.因此,只要有一个指向同一对象的指针,任何人都将看到该对象的所有属性更改,因为它们指向的是完全相同的对象.但是,如果您将新对象分配给master变量,则所有其他对象仍将具有指向先前对象而不是新对象的指针.

Primitives are copied when assigned. Objects are assigned by just sharing a pointer to the object (no copying). So anyone with a pointer to the same object will see all property changes on that object since they point at the exact same object. But, if you assign a new object to the master variable, then all others will still have a pointer to the previous object not the new object.

以下是一些例子来说明:

Here are a few examples to illustrate:

let master = {name: "John"};

// x gets pointer to the same object that master points to
// both master and x point to the same object
let x = master;
console.log(x.name);    // "John"

// update property on master
// but, both variables point to the same object so both see
// the change in the property value on that object
master.name = "Alice";
console.log(master.name);  // "Alice"
console.log(x.name);       // "Alice"
console.log(x === master); // true, they both point to the same object

// reassign a new object to master variable
master = {name: "Drew"};

// x still points at original object that was originally in master
// and knows nothing about the whole new object in master now
console.log(master.name);  // "Drew" 
console.log(x.name);       // still "Alice", not "Drew"
console.log(x === master); // false, not the same object


原始数据类型(例如数字或布尔值)是通过复制分配的,因此在将一个变量的内容分配给另一个变量之后,两个变量之间根本没有连接.它们每个都有自己的数据内容,因此更改一个变量的值不会影响另一变量的内容.


Primitive data types such as numbers or booleans are assigned by copy so after assigning the contents of one variable to another, there is no connection at all between the two variables. They each have their own content of the data so changing the value of one variable has no effect on the content of the other variable.

 // primitive data value
 let master = 2;

 let x = master;
 console.log(x);     // 2

 master = 10;
 console.log(x);     // still 2

这篇关于全局变量赋值在NodeJS上是原子的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆