javascript 如何保留共享节点集群中所有节点进程的变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14826349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:40:55  来源:igfitidea点击:

how to keep variables that share all node processes in node cluster?

javascriptnode.jsnode-cluster

提问by lahiru madhumal

It seems like all the node woker processes are working as if it is executing a new copy of the same application. But would like to keep some variables that are shared by all node workers (child processes) in node cluster. Is there a simple way to do this?

似乎所有节点唤醒进程都在工作,就好像它正在执行同一应用程序的新副本一样。但是想保留一些由节点集群中的所有节点工作人员(子进程)共享的变量。有没有一种简单的方法可以做到这一点?

采纳答案by Floby

All worker processes are indeed new copies of your application. Each worker is a full featured process created with child_process.spawn. So no, they don't share variables. And it's probably best this way. If you want to share information between worker processes (typically sessions) you should look into storing these information in a database.

所有工作进程确实是您应用程序的新副本。每个 worker 都是一个由 child_process.spawn 创建的全功能进程。所以不,他们不共享变量。这种方式可能是最好的。如果您想在工作进程(通常是会话)之间共享信息,您应该考虑将这些信息存储在数据库中。

If you're ready to go node all the way, you could use something like dnodeto have your workers ask the master process for data.

如果您准备好一路使用 node,您可以使用dnode 之类的东西让您的工作人员向主进程询问数据。

回答by hWndepT

You can try to communicate between master process and child processes. For example:

您可以尝试在主进程和子进程之间进行通信。例如:

script test.master.js:

脚本 test.master.js:

var cluster = require('cluster');
var childScript = __dirname + '/test.child.js';

cluster.setupMaster({ exec: childScript });

proc = cluster.fork();
proc.on('message', function(message) {
    console.log('message from child: ', message);
    proc.send('Hello from master!');
});

script test.child.js:

脚本 test.child.js:

console.log('Child initializing..');

process.on('message', function(message) {
    console.log('message from master: ', message);
});

process.send('Hello from Child!');

回答by vodolaz095

I used external memcached or redis server for it.

我为此使用了外部 memcached 或 redis 服务器。

回答by srosh

I think the whole idea of cluster is having instances that can run independently on different cpus. Sharing memory (a global variable) that they both can access and change introduces more complexity (locks, etc) and makes these instances depend on one another.

我认为集群的整个想法是拥有可以在不同 CPU 上独立运行的实例。共享它们都可以访问和更改的内存(一种全局变量)会引入更多的复杂性(锁等)并使这些实例相互依赖。

An outside database would be a good solution to this since it takes care of all th data access problems, but it most likely lowers performance.

外部数据库将是一个很好的解决方案,因为它会处理所有数据访问问题,但很可能会降低性能。

Messaging is a better idea. You can hold local instances of a var in your clusters. When a cluster updates the value, send a message to the rest of them and update the value. This is great because it's async and nonblocking but again your value update won't reflect immediately.

消息传递是一个更好的主意。您可以在集群中保存 var 的本地实例。当集群更新值时,向其余集群发送消息并更新值。这很棒,因为它是异步和非阻塞的,但同样,您的值更新不会立即反映出来。

How about this: store vars on a db and everytime there's a value change notify the instances. They can store the new values in local vars and make db calls only when it's needed

怎么样:将变量存储在数据库上,每次有值更改时通知实例。他们可以将新值存储在本地变量中并仅在需要时进行数据库调用

回答by Allen Luce

If you're looking to share things on a read-only basis, check out mmap-object. I use it for large in-memory lookup tables.

如果您希望以只读方式共享内容,请查看mmap-object。我将它用于大型内存查找表。

Checking just now on a server, a 346M file is taking up a total of 156M of memory (mmap only pages in what's accessed) and mmap-object sharing it among 44 processes for a per-process overhead of 3.5M.

刚刚在服务器上检查,一个 346M 的文件总共占用了 156M 的内存(mmap 仅在访问的页面中)并且 mmap-object 在 44 个进程之间共享它,每个进程的开销为 3.5M。

Because it's read-only I don't have to worry about inter-process locking and the messiness that can bring along.

因为它是只读的,所以我不必担心进程间锁定和可能带来的混乱。

回答by Mauvis Ledford

Nobody has mentioned this yet but this is a perfect case for Node Worker Threadswhich just got out of experimental mode in the latest version of Node v11.11.0.

还没有人提到这一点,但这是Node Worker Threads 的一个完美案例,它在最新版本的 Node v11.11.0 中刚刚退出实验模式。