简体   繁体   English

node.js中的集群模块

[英]Cluster module in node.js

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

if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);

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

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {
  // Workers can share any TCP connection
  // In this case it is an HTTP server
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('hello world\n');
  }).listen(8000);

  console.log(`Worker ${process.pid} started`);
}

We spawn as many number of workers as there are cores in system.我们产生的工人数量与系统中的核心数量一样多。 so for 4 cores, we spawn 4 workers in master process.所以对于 4 个核心,我们在主进程中产生 4 个工作人员。 Then where exactly master process runs, ie on which core?那么主进程到底在哪里运行,即在哪个核心上? And since master routes request to workers, how is it ensured that master is always running and listening on a port?并且由于 master 将请求路由到 worker,如何确保 master 始终在运行并侦听端口?

That's a part of cpu scheduling ,os will manage which core should run which process.这是cpu scheduling的一部分,操作系统将管理哪个内核应该运行哪个进程。

You can create more child processes than your available cpus (which is not suggested ) so os will handle which cpu core will run which process,master process can share cpu with some child process.您可以创建比可用cpus更多的子进程(不建议这样做),因此os将处理哪个 cpu 核心将运行哪个进程,主进程可以与某些子进程共享 cpu。

We spawn as many number of workers as there are cores in system.我们产生的工人数量与系统中的核心数量一样多。 so for 4 cores, we spawn 4 workers in master process.所以对于 4 个核心,我们在主进程中产生 4 个工作人员。 Then where exactly master process runs, ie on which core?那么主进程到底在哪里运行,即在哪个核心上?

Process assignment to the core is done by OS based on certain params.内核的进程分配是由操作系统根据某些参数完成的。 However, if you are not running the cluster module Node.js runs the single thread and if you run the Node.js application with cluster the main thread becomes master and you can spawn child processes based on your OS and hardware configuration, not going deep in that.但是,如果您没有运行集群模块 Node.js 运行单线程,并且如果您使用集群运行 Node.js 应用程序,则主线程成为主线程,您可以根据您的操作系统和硬件配置生成子进程,而不是深入。

And since master routes request to workers, how is it ensured that master is always running and listening on a port?并且由于 master 将请求路由到 worker,如何确保 master 始终在运行并侦听端口?

Sometimes it happens that your master process is killed, but the child process won't receive signal.有时会发生您的主进程被杀死,但子进程不会收到信号。 So, they do run.所以,他们确实跑了。 However, you can handle such scenario by using simple code:但是,您可以使用简单的代码来处理这种情况:

const k = cp.spawn();
childPids.push(k.pid)

Or,或者,

const k = cp.fork();
childPids.push(k.pid)

And with the pid you can kill the child process.使用 pid,您可以终止子进程。 This is something you need to do when your child process won't killed.当您的子进程不会被杀死时,这是您需要做的事情。 There could be lot of scenarios can't cover all of them, but I tried to give a general overview to avoid such situations.可能有很多场景无法涵盖所有场景,但我试图给出一个总体概述以避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM