Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Node.js Cluster Process Module

❮ Built-in Modules


Example

Run the code three times, the first time is as a master, then as workers:
var cluster = require('cluster');

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

Definition and Usage

The cluster module provides a way of creating child processes that runs simultaneously and share the same server port.

Node.js runs single threaded programming, which is very memory efficient, but to take advantage of computers multi-core systems, the Cluster module allows you to easily create child processes that each runs on their own single thread, to handle the load.


Syntax

The syntax for including the cluster module in your application:

var cluster = require('cluster');

Cluster Properties and Methods

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

❮ Built-in Modules

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.