Skip to content

Instantly share code, notes, and snippets.

@JacobHsu
Last active August 29, 2015 14:10
Show Gist options
  • Save JacobHsu/fc97ee158d0ff9d6cdd2 to your computer and use it in GitHub Desktop.
Save JacobHsu/fc97ee158d0ff9d6cdd2 to your computer and use it in GitHub Desktop.
pollingLoop();
function pollingLoop(){
var cp = require('child_process');
var child = cp.fork('./worker');
var num = Math.floor((Math.random() * 100) + 1);
child.on('message', function(m) {
// Receive results from child process
console.log('Child process started: %d', child.pid);
console.log('received: ' + m);
});
// Send child process some work
child.send({ word: 'Please up-case this string', random: num});
setTimeout(pollingLoop, 3000);
}
process.on('message', function(m) {
// Do work (in this case just up-case the string
word = m.word.toUpperCase();
num = m.random;
// Pass results back to parent process
process.send(word+' '+num);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment