我应该在node.js中fork()多少个child_processes? [英] How many child_processes should I fork() in node.js?

查看:424
本文介绍了我应该在node.js中fork()多少个child_processes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单。但是,它可能需要不同的变量来回答(我猜)

My question is quite simple. though, it may require different variable to be answered (i guess)

我在使用node.js,我正在想如何使用它多核架构。

I'm playing around with node.js and I'm thinking of how to use it in a multi core architecture.

最新版本提供 child_process.fork() child.spawn ()多进程编程的方法。我已阅读关于使用Node.js作为大型Comet服务器的这篇非常好(但是过时)的文章。现在nodejs提供多进程编程,我真的不知道我应该产生多少进程服务大量的请求(假设我的服务器只运行在一台机器上)。是否有一种方法可以选择执行相同作业的最佳(或至少个好的)数量的子进程?

Latest version provides child_process.fork()and child.spawn() methods for multi-process programming. I've read on this very good (but dated) article about using Node.js as a large-scale Comet server. Now then nodejs provides multi-process programming, I really have no idea about how many processes should I have to spawn for serving large number of request (assuming my server is running on just one machine). Is there a way of choosing the 'best' (or at least a good) number of child processes doing the same job?

任何链接到起点指南都将非常感谢。

Any link to a starting guide would be very appreciated.

谢谢

推荐答案

Node.js官方文档有一个如何使用集群的示例:

The Node.js official documentation has an example of how to use cluster:

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

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('death', function(worker) {
    console.log('worker ' + worker.pid + ' died');
  });
} else {
  // Worker processes have a http server.
  http.Server(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}

正如你在上面的代码中可以看到的,您拥有的cpus数量,以便所有核心工作在同一时间(在处理器核心之间分配工作)。

As you can see in the code above, you should use as many forks as the number of cpus you have, so that all cores work in the same time (distribute the work between your processor's cores).

这篇关于我应该在node.js中fork()多少个child_processes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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