Created
June 6, 2012 04:59
-
-
Save anonymous/2880020 to your computer and use it in GitHub Desktop.
stdin
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 cluster = require('cluster'); | |
if(cluster.isMaster) { | |
addLoop = function(newloop, delay) { | |
console.log('Adding ' + newloop.toString()); | |
var worker = cluster.fork(); | |
cluster.on('death', function(worker) { | |
console.log('Loop with PID ' + worker.pid + ' died'); | |
}); | |
worker.on('message', function(m) { | |
console.log('Parent got message ' + JSON.stringify(m)); | |
if(m.getfunction) { | |
var message = { myfunction: newloop.toString(), delay: 2000 }; | |
console.log('Sending message ' + JSON.stringify(message)); | |
worker.send(message); | |
} | |
}); | |
process.on('SIGTERM', function() { | |
console.log('Parent got SIGTERM'); | |
worker.kill(); | |
}); | |
}; | |
var function1 = function() { | |
console.log('Running function1'); | |
} | |
addLoop(function1); | |
} else { | |
process.send({ getfunction: true }); | |
process.on('message', function(m) { | |
console.log('Child got message ' + JSON.stringify(m)); | |
console.log('typeof m.myfunction = ' + typeof m.myfunction); | |
var myfunction = function() {}; | |
eval('myfunction = ' + m.myfunction); | |
var repeat = function repeat() { | |
myfunction(); | |
if(m.delay) setTimeout(repeat, m.delay); | |
else process.nextTick(repeat); | |
}; | |
repeat(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment