티스토리 뷰
Nodejs multi core cluster 사용하기.
Single Thread 기반의 Nodejs지만 cluster를 사용하여 Multi Thread형태의 구조로 동작이 가능하게 할 수 있습니다.
바로 node cluster를 이용하면 됩니다.
간단한 예제 코드를 통해 확인 해봅니다.
app.js
var cluster = require('cluster'); var numCPUs = require('os').cpus().length; var express = require('express'); //Parent Process if( cluster.isMaster ){ var workers = {}; for (var i=0; i < numCPUs; i++) spawn(); function spawn() { worker = cluster.fork(); workers[worker.id] = worker; console.log(worker.process.pid + ' is Online'); worker.on('message', messageHandler); } function messageHandler(msg) { if (msg.cmd && msg.cmd == 'rocksea') { for (var worker in workers) { workers[worker].process.send({ cmd : 'rocksea' }); } } } cluster.on('exit', function(worker) { console.log('worker ' + worker.process.pid + ' exited'); delete workers[worker.id]; spawn(); }); } else { // Child process. process.on('message', function(msg) { if (msg.cmd) { if (msg.cmd == 'rocksea') { console.log("################### Rocksea IPC Call"); console.log('Worker ' + cluster.worker.id + ' running!'); } } }); var app = express(); app.listen(3333); app.get('/', function (req, res) { res.send('Hello World!! Cluster is '+ cluster.worker.id); //IPC Call process.send({ cmd : "rocksea"}); }); console.log("############## Process Running!!");
위 예제 코드로 테스트 할 수 있는 것들은 아래의 두가지 입니다.
1. Fork를 사용하여 Child Process를 생성.
2. process.send를 이용하여 Process간 IPC 통신 테스트
Single Thread의 경우 Process Tree
Multi Thread의 경우 Process Tree
worker가 2배로 늘어나 있는것을 확인 해 볼 수 있으며 Throughput 또한 이와 비례하여 늘어 나는 것을 확인 할 수 있습니다.
부족한 부분은 아래의 링크를 참조하시기 바랍니다.
참조 URL : http://rowanmanning.com/posts/node-cluster-and-express/
.by rocksea
'Developer' 카테고리의 다른 글
[PHP] pear 란. (0) | 2013.10.15 |
---|---|
파일 업로드 다운로드 취약점 (2) | 2013.10.10 |
[jqeury] 자동완성 구현 ( auto-complete ) (2) | 2013.09.09 |
[nginx] 자동 urldecoding문제 (0) | 2013.08.22 |
[chatting] redis pub/sub + socket.io를 이용한 chatting server & client (7) | 2013.08.09 |
- Total
- Today
- Yesterday
- 도덕경
- 해외여행
- 스페인 여행
- PostgreSQL
- 대명사 구문
- redis
- hadoop
- Python Django
- 영작
- 가정법
- AWS
- k8s
- 베트남
- Python
- Business English
- JBOSS
- it
- 다낭
- NGINX
- mongoDB
- maven
- 조동사
- 비교구문
- memcached
- nodejs
- 비지니스 영어
- 영문법
- ubuntu
- 여행
- hdfs
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |