Created
January 4, 2015 12:21
-
-
Save brugnara/827f64c216c5325aea4d to your computer and use it in GitHub Desktop.
fork vs spawn, nodejs
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 i = 0; | |
setInterval(function() { | |
if (i++ % 2) { | |
console.log('I\'m the child!'); | |
} else { | |
console.error('I\'m the child!'); | |
} | |
}, 1500); |
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 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); |
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 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); |
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
Between fork and spawn which is better?