Skip to content

Instantly share code, notes, and snippets.

@brugnara
Created January 4, 2015 12:21
Show Gist options
  • Save brugnara/827f64c216c5325aea4d to your computer and use it in GitHub Desktop.
Save brugnara/827f64c216c5325aea4d 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);
@AhmedCommando
Copy link

depends on the use case, fork create a copy of the v8 engine instance with copy of node_modules, and spawn contains fork and create an entire new instance of V8 engine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment