Node.js Cluster Process Module

❮ โมดูลในตัว


ตัวอย่าง

รันโค้ดสามครั้ง ครั้งแรกเป็นมาสเตอร์ จากนั้นในฐานะผู้ปฏิบัติงาน:
var cluster = require('cluster');

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

ความหมายและการใช้งาน

โมดูลคลัสเตอร์มีวิธีการสร้างกระบวนการลูกที่ทำงานพร้อมกันและใช้พอร์ตเซิร์ฟเวอร์เดียวกันร่วมกัน

Node.js เรียกใช้การเขียนโปรแกรมแบบเธรดเดียว ซึ่งมีประสิทธิภาพหน่วยความจำมาก แต่เพื่อใช้ประโยชน์จากระบบมัลติคอร์ของคอมพิวเตอร์ โมดูลคลัสเตอร์ช่วยให้คุณสร้างกระบวนการย่อยที่แต่ละกระบวนการทำงานบนเธรดเดี่ยวของตนเอง เพื่อจัดการกับโหลดได้อย่างง่ายดาย


ไวยากรณ์

ไวยากรณ์สำหรับการรวมโมดูลคลัสเตอร์ในแอปพลิเคชันของคุณ:

var cluster = require('cluster');

คุณสมบัติและวิธีการของคลัสเตอร์

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

❮ โมดูลในตัว