Created
June 2, 2012 04:22
-
-
Save mlin/2856550 to your computer and use it in GitHub Desktop.
Cannot spawn a child process from within a cluster worker in Node v0.6.18+
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
/* | |
$ uname -a | |
Linux coderuler 3.2.0-24-generic #39-Ubuntu SMP Mon May 21 16:52:17 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux | |
$ node -v | |
v0.6.18 | |
$ echo "console.log('foobar'); process.exit(0);" > foo.js | |
$ node node_cluster_child_process.js | |
child stderr > | |
child stderr > cluster.js:215 | |
process.send(msg); | |
^ | |
child stderr > TypeError: Object #<EventEmitter> has no method 'send' | |
at queryMaster (cluster.js:215:11) | |
at EventEmitter._startWorker (cluster.js:184:3) | |
at startup (node.js:91:17) | |
at node.js:551:3 | |
Child process exited with code 11 | |
Worker exiting | |
Master exiting | |
*/ | |
var cluster = require('cluster'); | |
if (cluster.isMaster) { | |
cluster.fork(); | |
cluster.on('death', function() { | |
console.error('Master exiting'); | |
process.exit(0); | |
}); | |
} | |
else { | |
var child_process = require('child_process'); | |
var args = ['foo.js']; | |
// uncomment to show that the problem seems to arise only when | |
// attempting to access the file system: | |
//args = ['-e','console.log("barbaz"); process.exit(0);']; | |
var ch = child_process.spawn('node', args); | |
ch.stdout.on('data', function(data) { | |
console.log('child stdout > ' + data.toString()); | |
}); | |
ch.stderr.on('data', function(data) { | |
console.log('child stderr > ' + data.toString()); | |
}); | |
ch.on('exit', function(code) { | |
console.error('Child process exited with code ' + code); | |
console.error('Worker exiting'); | |
process.exit(code); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to fix that?