-
-
Save euclid1990/782f112bff81b4e8311c67b37decfb4b to your computer and use it in GitHub Desktop.
fork vs spawn, nodejs
This file contains hidden or 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 hidden or 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 hidden or 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment