Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created April 28, 2013 12:44
Show Gist options
  • Save takaheraw/5476783 to your computer and use it in GitHub Desktop.
Save takaheraw/5476783 to your computer and use it in GitHub Desktop.
var argv2 = process.argv[2] || 10000;
var dly = (argv2 > 20000) ? 2000 : argv2;
setTimeout(function() {
process.send('作業終了。' + dly + 'ミリ秒かかりました。');
}, dly);
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