Как в nodeJS запустить несколько потоков чтения/записи одновременно?

Sanu0074
На сайте с 31.08.2012
Offline
110
442

Делаю загрузку файлов на сервер. Хочу использовать потоки.

Я попытался кое-что сделать:

//FileSystem handler library

var log = require("core/lib/log")(module);
var fs = require('fs');
var async = require('async');

var fsh = function (files) {

var $this = this;

this.moveUploadedFiles = function(dest,cb){
if(!Object.keys(files).length){
log.info('fsh.moveUploadedFiles-> files object is empty');
return cb({state:0}); //files not found
}

async.waterfall ([ function (cb) {
$this.createFolder(dest,function(){
cb(null);
});
}
], function () {
var infile = [];
var outfile = [];
var uploadSize = [];
for (var i in files) {
var path = dest + '/' + files.originalFilename;
infile = fs.createReadStream(files.path);
outfile = fs.createWriteStream(path);
infile.on('data',function(data) {
if(!uploadSize){
uploadSize = 0;
}
uploadSize += data.length/1024;
console.log(files.originalFilename + ': '+(files.size/1024) + '/'+uploadSize+' Kb');
outfile.write(data);
});
infile.on('close', function() {
outfile.close();
});
infile.pipe(outfile);
}
});

return cb({state:1}); //upload success
};

this.createFolder = function(dir,cb){
fs.exists(dir, function (exists) {
if(exists){
log.info('fsh.createFolder-> Dir exist: %s',dir);
return cb();
}else{
fs.mkdir(dir,777,function(){
log.info('fsh.createFolder-> %s',dir);
return cb();
});
}
});
};


};

module.exports = fsh;

но как-то неправильно работает, несколько файлов за раз не пишет, работает только с одним. Я выбрал два разных файла, в консоли вижу следующее:

Hawaii-Big-Island-TF.jpg: 1323.076171875/64 Kb

Hawaii-Big-Island-TF.jpg: 1323.076171875/128 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/192 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/256 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/320 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/384 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/448 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/512 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/576 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/640 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/704 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/768 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/832 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/896 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/960 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1024 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1088 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1152 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1216 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1280 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1344 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1408 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1472 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1536 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1600 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1664 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1728 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1792 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1856 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1920 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/1984 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2048 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2112 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2176 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2240 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2304 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2368 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2432 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2496 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2560 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2624 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2688 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2752 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2816 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2880 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/2944 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3008 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3072 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3136 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3200 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3264 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3328 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3392 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3456 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3520 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3584 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3597.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3661.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3725.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3789.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3853.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3917.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/3981.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4045.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4109.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4173.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4237.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4301.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4365.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4429.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4493.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4557.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4621.8701171875 Kb
Hawaii-Big-Island-TF.jpg: 1323.076171875/4664.9462890625 Kb

В чем может быть проблема?

Авторизуйтесь или зарегистрируйтесь, чтобы оставить комментарий