-
-
Save kpturner/0c7104b32ecca7689fc0 to your computer and use it in GitHub Desktop.
Forked child test
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 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) |
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
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