Skip to content

Instantly share code, notes, and snippets.

@euclid1990
Forked from brugnara/child.js
Created March 19, 2017 06:09
Show Gist options
  • Save euclid1990/782f112bff81b4e8311c67b37decfb4b to your computer and use it in GitHub Desktop.
Save euclid1990/782f112bff81b4e8311c67b37decfb4b to your computer and use it in GitHub Desktop.
fork vs spawn, nodejs
var i = 0;
setInterval(function() {
if (i++ % 2) {
console.log('I\'m the child!');
} else {
console.error('I\'m the child!');
}
}, 1500);
var fork = require('child_process').fork;
var child = fork('child.js');
child.on('close', function (code) {
if (code !== 0) {
console.log('child process exited with code ' + code);
}
});
setTimeout(function() {
throw new Error('ZEPELE');
}, 5000);
var spawn = require('child_process').spawn;
var child = spawn('node', ['child.js']);
child.stdout.on('data', function (data) {
console.log('stdin:', data.toString());
});
child.stderr.on('data', function (data) {
console.log('child stderr: ', data.toString());
});
child.on('close', function (code) {
if (code !== 0) {
console.log('child process exited with code ' + code);
}
});
setTimeout(function() {
throw new Error('ZEPELE');
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment