Skip to content

Instantly share code, notes, and snippets.

@kaiquewdev
Created February 27, 2013 21:45
Show Gist options
  • Select an option

  • Save kaiquewdev/5052043 to your computer and use it in GitHub Desktop.

Select an option

Save kaiquewdev/5052043 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
(function () {
'use strict';
var request = require('request'),
fs = require('fs'),
cluster = require('cluster'),
nCPUs = require('os').cpus().length;
var imgs = [
'http://upload.wikimedia.org/wikipedia/commons/a/af/Flag_of_South_Africa.svg',
'http://upload.wikimedia.org/wikipedia/commons/9/9a/Flag_of_Afghanistan.svg',
'http://upload.wikimedia.org/wikipedia/commons/thumb/3/36/Flag_of_Albania.svg/700px-Flag_of_Albania.svg.png',
'http://upload.wikimedia.org/wikipedia/commons/thumb/b/ba/Flag_of_Germany.svg/800px-Flag_of_Germany.svg.png'
], label = 'Time to get all files';
console.time( label );
if ( cluster.isMaster ) {
while ( nCPUs-- ) {
cluster.fork();
}
} else {
imgs.map(function ( img, i ) {
var filename = img.split('/')[ img.split('/').length - 1 ],
file = request( img ).pipe( fs.createWriteStream( filename ) );
if ( i === ( imgs.length - 1 ) ) {
process.exit();
}
});
}
console.timeEnd( label );
}).call( this );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment