在node.js中,如何声明一个可以被master进程初始化并被worker进程访问的共享变量? [英] In node.js, how to declare a shared variable that can be initialized by master process and accessed by worker processes?

查看:44
本文介绍了在node.js中,如何声明一个可以被master进程初始化并被worker进程访问的共享变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要以下内容

  • 在启动期间,主进程从文件加载一个大表并将其保存到共享变量中.该表有 9 列和 1200 万行,大小为 432MB.
  • 工作进程运行 HTTP 服务器,接受针对大表的实时查询.

这是我的代码,显然没有达到我的目标.

Here is my code, which obviously does not achieve my goal.

var my_shared_var;
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Load a large table from file and save it into my_shared_var,
  // hoping the worker processes can access to this shared variable,
  // so that the worker processes do not need to reload the table from file.
  // The loading typically takes 15 seconds.
  my_shared_var = load('path_to_my_large_table');

  // Fork worker processes
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
} else {
  // The following line of code actually outputs "undefined".
  // It seems each process has its own copy of my_shared_var.
  console.log(my_shared_var);

  // Then perform query against my_shared_var.
  // The query should be performed by worker processes,
  // otherwise the master process will become bottleneck
  var result = query(my_shared_var);
}

我尝试将大表保存到 MongoDB 中,以便每个进程都可以轻松访问数据.但是表的大小太大了,即使有索引,MongoDB 也需要大约 10 秒才能完成我的查询.这对于我的实时应用程序来说太慢并且无法接受.我也试过 Redis,它在内存中保存数据.但是 Redis 是一个键值存储,而我的数据是一个表.我还写了一个C++程序将数据加载到内存中,查询时间不到1秒,所以我想在node.js中模拟这个.

I have tried saving the large table into MongoDB so that each process can easily access to the data. But the table size is so huge that it takes MongoDB about 10 seconds to complete my query even with an index. This is too slow and not acceptable for my real-time application. I have also tried Redis, which holds data in memory. But Redis is a key-value store and my data is a table. I also wrote a C++ program to load the data into memory, and the query took less than 1 second, so I want to emulate this in node.js.

推荐答案

您正在寻找共享内存,其中 node.js 只是不支持.您应该寻找替代方案,例如 查询数据库 或使用 内存缓存.

You are looking for shared memory, which node.js just does not support. You should look for alternatives, such as querying a database or using memcached.

这篇关于在node.js中,如何声明一个可以被master进程初始化并被worker进程访问的共享变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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