Skip to content

Instantly share code, notes, and snippets.

@erhangundogan
Created January 7, 2013 06:19
Show Gist options
  • Save erhangundogan/4472898 to your computer and use it in GitHub Desktop.
Save erhangundogan/4472898 to your computer and use it in GitHub Desktop.
// child.js
console.log('child was created with PID: ' + process.pid);
process.on('exit', function(code,signal) {
console.warn('child is exiting with code: '+ code +' and signal: '+signal)
});
process.on('message', function(data) {
console.log('child: got message from parent: ' + data);
process.send('hi, thanks for the message!');
});
// fork.js
var fork = require('child_process').fork,
child = fork(__dirname + '/child.js');
child.on('message', function(data) {
console.log('parent: got message from child: ' + data);
});
child.send('hello from your parent');
// server.js
function format(type, output) {
console.log('child says (' + type + '): ' + output);
}
var spawn = require('child_process').spawn,
child = spawn('node',['child.js']);
child.stdout.on('data', format.bind(global,'stdout'));
child.stderr.on('data', format.bind(global,'stderr'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment