Módulo de proceso de clúster de Node.js

❮ Módulos incorporados


Ejemplo

Ejecute el código tres veces, la primera vez como maestro, luego como trabajadores:
var cluster = require('cluster');

if (cluster.isWorker) {
  console.log('I am a worker');
} else {
  console.log('I am a master');
  cluster.fork();
  cluster.fork();
}

Definición y uso

El módulo de clúster proporciona una forma de crear procesos secundarios que se ejecutan simultáneamente y comparten el mismo puerto de servidor.

Node.js ejecuta programación de subproceso único, que es muy eficiente en cuanto a la memoria, pero para aprovechar los sistemas multinúcleo de las computadoras, el módulo Cluster le permite crear fácilmente procesos secundarios que cada uno se ejecuta en su propio subproceso único, para manejar la carga.


Sintaxis

La sintaxis para incluir el módulo de clúster en su aplicación:

var cluster = require('cluster');

Propiedades y métodos del clúster

Method Description
disconnect() Disconnects all workers
exitedAfterDisconnect Returns true if a worker was exited after disconnect, or the kill method
fork() Creates a new worker, from a master
id A unique id for a worker
isConnected Returns true if the worker is connected to its master, otherwise false
isDead Returns true if the worker's process is dead, otherwise false
isMaster Returns true if the current process is master, otherwise false
isWorker Returns true if the current process is worker, otherwise false
kill() Kills the current worker
process Returns the global Child Process
schedulingPolicy Sets or gets the schedulingPolicy
send() sends a message to a master or a worker
settings Returns an object containing the cluster's settings
setupMaster() Changes the settings of a cluster
worker Returns the current worker object
workers Returns all workers of a master

❮ Módulos incorporados