Node.js 클러스터 프로세스 모듈
예시
코드를 세 번 실행합니다. 처음에는 마스터로, 그 다음에는 작업자로 실행합니다.
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 |