Created
January 17, 2012 06:23
-
-
Save kawaz/1625143 to your computer and use it in GitHub Desktop.
Node v0.7.0出たのでIsolatesのテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var child_process = require('child_process'); | |
var cpuCount = require('os').cpus().length; | |
if(process.features.isolates) { | |
if(process.tid == 1) { | |
//main thread | |
console.log("Isolates is supported! (" + process.version + ")"); | |
for(var i = 0; i < 5; i++) { //ここでは試験用に固定値だが、CPU使いきりたいならcpuCount使うと良いか | |
var child = child_process.fork(__filename, null, {thread: true}); | |
child.send("Hello thread"); | |
child.on('message', function(m) { | |
console.log(m); | |
}); | |
} | |
} else { | |
//child thread | |
process.on('message', function(m) { | |
process.send("receive [" + m + "]. pid:" + process.pid + ", tid:" + process.tid); | |
}); | |
setTimeout(function(){ | |
//実行中のプロセスツリーを確認するために5秒生きる | |
console.log("I die. pid:" + process.pid + ", tid:" + process.tid); | |
}, 5000); | |
} | |
} else { | |
if(!process.env.forked) { | |
//main process | |
console.log("Isolates is not supported! (" + process.version + ")"); | |
for(var i = 0; i < 5; i++) { //ここでは試験用に固定値だが、CPU使いきりたいならcpuCount使うと良いか | |
var child = child_process.fork(__filename, null, {env: {forked:1}}); | |
child.send("Hello process"); | |
child.on('message', function(m) { | |
console.log(m); | |
}); | |
} | |
} else { | |
//child process | |
process.on('message', function(m) { | |
process.send("receive [" + m + "]. pid:" + process.pid + ", tid:" + process.tid); | |
}); | |
setTimeout(function(){ | |
//実行中のプロセスツリーを確認するために5秒生きる | |
console.log("I die. pid:" + process.pid + ", tid:" + process.tid); | |
process.exit(); | |
}, 5000); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ nvm use v0.7.0 | |
$ node isolates-example.js | |
Isolate is supported! (v0.7.0) | |
receive [Hello thread]. pid:30300, tid:2 | |
receive [Hello thread]. pid:30300, tid:6 | |
receive [Hello thread]. pid:30300, tid:5 | |
receive [Hello thread]. pid:30300, tid:3 | |
receive [Hello thread]. pid:30300, tid:4 | |
I die. pid:30300, tid:2 | |
I die. pid:30300, tid:6 | |
I die. pid:30300, tid:5 | |
I die. pid:30300, tid:3 | |
I die. pid:30300, tid:4 | |
$ nvm use v0.6.7 | |
$ node isolates-example.js | |
Isolate is not supported! (v0.6.7) | |
receive [Hello process]. pid:7528, tid:undefined | |
receive [Hello process]. pid:7530, tid:undefined | |
receive [Hello process]. pid:7527, tid:undefined | |
receive [Hello process]. pid:7526, tid:undefined | |
receive [Hello process]. pid:7529, tid:undefined | |
I die. pid:7528, tid:undefined | |
I die. pid:7530, tid:undefined | |
I die. pid:7527, tid:undefined | |
I die. pid:7526, tid:undefined | |
I die. pid:7529, tid:undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ps f #v0.7.0 | |
22526 pts/0 Ss 0:00 -bash | |
30300 pts/0 Sl+ 0:00 \_ node isolates-example.js | |
$ ps f #v0.6.7 | |
22526 pts/0 Ss 0:00 -bash | |
7512 pts/0 Sl+ 0:00 \_ node isolates-example.js | |
7526 pts/0 Sl+ 0:00 \_ /home/kawaz/.nvm/v0.6.7/bin/node /tmp/isolates-example.js | |
7527 pts/0 Sl+ 0:00 \_ /home/kawaz/.nvm/v0.6.7/bin/node /tmp/isolates-example.js | |
7528 pts/0 Sl+ 0:00 \_ /home/kawaz/.nvm/v0.6.7/bin/node /tmp/isolates-example.js | |
7529 pts/0 Sl+ 0:00 \_ /home/kawaz/.nvm/v0.6.7/bin/node /tmp/isolates-example.js | |
7530 pts/0 Sl+ 0:00 \_ /home/kawaz/.nvm/v0.6.7/bin/node /tmp/isolates-example.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment