Created
April 28, 2013 12:44
-
-
Save takaheraw/5476783 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 argv2 = process.argv[2] || 10000; | |
var dly = (argv2 > 20000) ? 2000 : argv2; | |
setTimeout(function() { | |
process.send('作業終了。' + dly + 'ミリ秒かかりました。'); | |
}, dly); |
This file contains hidden or 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 cp = require('child_process'); | |
var http = require('http'); | |
var host = '192.168.11.20'; | |
var port = 3333; | |
http.createServer(function(req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'}); | |
var dly = parseInt(Math.random()*10000); | |
if (dly > 3000) { | |
forkChilds(function(child, msg) { | |
res.end('' + msg); | |
child.kill(); | |
}, dly); | |
} else { | |
res.end('お待たせしません!'); | |
} | |
}).listen(port, host); | |
function forkChilds(callback, dly) { | |
var child = cp.fork(__dirname + '/child.js', [dly]); | |
console.log('forked pid: ' + child.pid); | |
child.on('message', function(msg) { | |
callback(child, msg); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment