Skip to content

Instantly share code, notes, and snippets.

@kpturner

kpturner/main.js Secret

Created October 15, 2015 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kpturner/0c7104b32ecca7689fc0 to your computer and use it in GitHub Desktop.
Save kpturner/0c7104b32ecca7689fc0 to your computer and use it in GitHub Desktop.
Forked child test
var cp = require('child_process');
var child = cp.fork(__dirname + '/worker');
child.on('message', function(m) {
// Receive results from child process
console.log('received: ' + m);
});
child.on("close", function(){
console.log("The child process has terminated for no reason!")
})
process.on('uncaughtException', function (er) {
console.error(er.stack)
})
// Send child process some work
setInterval(function(){
console.log("Sending message to worker...")
child.send('Please upper-case this string')
},1000)
console.log("I'm the child process (worker) and I have started and I should never terminate")
process.on('message', function(m) {
// Do work (in this case just up-case the string
m = m.toUpperCase();
// Pass results back to parent process
process.send(m.toUpperCase(m));
});
process.on('disconnect', function() {
console.log('Worker: I have been disconnected??!')
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment