2016-08-12 6 views
1

동시에 100 개 이상의 파일을 다운로드하려고합니다. 그러나 다운로드 기능을 실행할 때 맥북은 윈도우즈에서 다운받을 수는 없지만 (새로운 작업을 실행할 수 없음), 두 가지 경우 모두 유휴 네트워크에서는 다운로드가 진행되지 않는다.Node.JS가 동시에 수백 개의 파일을 다운로드 중

var express = require('express'); 
var router = express.Router(); 
var fs = require('fs'); 
var youtubedl = require('youtube-dl'); 
var links = require('../models/Links'); 

router.get('/', function (req, res, next) { 
    links.find({dlStatus: false}, function (err, docs) { 

     if (err) { 
      console.log(err); 
      res.end(); 
     } else if (!docs) { 
      console.log('No incomplete downloads!'); 
      res.end(); 
     } else { 
      for (var i = 0; i < docs.length; i++) { 

       //todo scraping 


       var video = youtubedl(docs[i].url, [], {cwd: __dirname}); 

       // Will be called when the download starts. 
       video.on('info', function (info) { 
        console.log('Download started'); 
        console.log(info); 
       }); 

       video.pipe(fs.createWriteStream('./downloads/' + docs[i].id + '-' + i + '.mp4')); 
       video.on('complete', function complete(info) { 
        links.findOneAndUpdate({url: info.webpage_url}, {dlStatus: true}, function (err, doc) { 
         if (err)console.log(err); 
         else console.log('Download completed!') 
        }); 
       }); 
      } 
     } 
    }); 
}); 

module.exports = router; 

지금 누군가가 나를 여기에 도와주세요 수 있습니다 여기에

내 다운로드 모듈입니다? 파일을 다운로드 할 때 this 모듈을 사용하고 있습니다.

+1

단일 다운로드 작업 중 ...? – vkstack

+0

네, 동시에 5 번 다운로드를 시도했는데, 너무 효과가있었습니다. 이제 101 번으로 시도하고 있습니다. –

+0

async.That을 사용하여 시도해보십시오. – vkstack

답변

2

이 경우 솔루션은 비동기을 사용합니다.

async.each()

var express = require('express'); 
var router = express.Router(); 
var fs = require('fs'); 
var youtubedl = require('youtube-dl'); 
var links = require('../models/Links'); 
var async = require('async') 

router.get('/', function (req, res, next) { 
    links.find({dlStatus: false}, function (err, docs) { 

     if (err) { 
      console.log(err); 
      res.end(); 
     } else if (!docs) { 
      console.log('No incomplete downloads!'); 
      res.end(); 
     } else { 
      async.each(docs,function(doc,cb){ 
       var video = youtubedl(doc.url, [], {cwd: __dirname}); 

       // Will be called when the download starts. 
       video.on('info', function (info) { 
        console.log('Download started'); 
        console.log(info); 
       }); 

       video.pipe(fs.createWriteStream('./downloads/' + docs.id + '-' + i + '.mp4')); 
       video.on('complete', function complete(info) { 
        links.findOneAndUpdate({url: info.webpage_url}, {dlStatus: true}, function (err, doc) { 
         if (err){ 
         console.log(err); 
         cb(err); 
         } 
         else { 
         console.log('Download completed!'); 
         cb() 
         } 
        }); 
       }); 

      },function(err){ 
       if(err) 
       return console.log(err); 
       console.log("Every thing is done,Here!!"); 
      }) 
     } 
    }); 
}); 

module.exports = router; 

로 .... 그것은이 방법을 시도하고 당신은 배치가 너무 async.eachLimits()을 사용하여 모든 일을 처리 할 수 ​​있습니다.

+0

감사합니다. 오류를 처리하는 것을 잊었습니다. 두 코드 모두 잘 작동합니다 :) –