Skip to content

Instantly share code, notes, and snippets.

@shinout
Created November 10, 2011 07:20
Show Gist options
  • Save shinout/1354330 to your computer and use it in GitHub Desktop.
Save shinout/1354330 to your computer and use it in GitHub Desktop.
redirecting stdout goes wrong when writing stdout from multi-processes
var fork = require("child_process").fork;
var workers = [];
for (var i=0; i<6; i++) { (function(i) {
workers[i] = fork(__dirname + "/worker.js");
workers[i].send(i);
})(i) }
process.on("message", function(msg) {
for (var i=1; i<=100; i++) {
console.log(msg, i);
}
process.stdout.end();
});
process.stdout.on("close", function() {
process.exit();
});
@shinout
Copy link
Author

shinout commented Nov 10, 2011

$ node main.js > filename
$ wc filename
570 1140 2801 filename
(expects 600)

$ node main.js | wc
600 1200 2952

$ node main.js | cat > filename
$ wc filename
600 1200 2952

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