Skip to content

Instantly share code, notes, and snippets.

@yukidarake
Created December 13, 2011 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukidarake/1471662 to your computer and use it in GitHub Desktop.
Save yukidarake/1471662 to your computer and use it in GitHub Desktop.
optimist で いい感じに spawn させる例
var spawn = require('child_process').spawn;
var argv = require('optimist')
.usage('Usage: $0 -c [num] [cmd]\nExample: $0 -c 4 ls ..')
.demand('c')
.alias('c', 'concurrency')
.argv;
console.time(argv.$0);
for (var i = 1; i <= argv.c; i++) {
(function(n) {
process.env.CHILD_ID = n;
var child = spawn(argv._[0], argv._.slice(1));
child.stdout.setEncoding('utf8');
child.stdout.on('data', function(data) {
process.stdout.write(data);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function(data) {
process.stderr.write(data);
});
child.on('exit', function(code) {
console.log('exit', code);
});
})(i);
}
process.on('exit', function () {
console.timeEnd(argv.$0);
});
process.on('uncaughtException', function (err) {
try {
console.error(err);
} catch (e) {
console.log(e);
} finally {
process.exit(1);
}
});
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment