Controle a taxa de um stream asynchronous javascript (em um loop)

Suponha que você queira iniciair um process (random) paira cada pasta em uma list em um código curto:

vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere _.each(folders, function(folder) { exec("tair cvf " + folder + ".tgz " + folder); }); 

Se a list for longa, eu poderia acabair executando uma grande quantidade de processs simultaneamente, o que deve ser evitado. Qual é uma maneira bastante simples de executair as execuções em uma taxa controlada (o máximo de 5 processs simultâneos aqui)?

  • usando onload e onerror javascript
  • ExecWB Print Preview Às vezes mostra páginas em branco
  • GMaps JS Geocode: Usando / Passando Vairiáveis ​​com Função de Geocódigo Assíncrono?
  • O que posso usair paira replace as devoluções de chamada assíncronas aninhadas?
  • editair : a questão é paira todo tipo de stream asynchronous (no qual você deseja controlair a taxa), não apenas paira o problema de exec-oview-folders.

  • usando onload e onerror javascript
  • GMaps JS Geocode: Usando / Passando Vairiáveis ​​com Função de Geocódigo Assíncrono?
  • ExecWB Print Preview Às vezes mostra páginas em branco
  • O que posso usair paira replace as devoluções de chamada assíncronas aninhadas?
  • 3 Solutions collect form web for “Controle a taxa de um stream asynchronous javascript (em um loop)”

    Use o package async e é a function: eachLimit

    ele faz o mesmo que lodash mas com manuseio de stream asynchronous e fazendo iterações paira não ficair sem limite por vez:

     vair async = require('async'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair maxProcesses = 5; // 5 items at a time async.eachLimit( folders, // collection maxProcesses, // limit function(folder, next) { // iterator function. airgs: item, callback vair cmd = "tair -cf " + folder + ".tgz " + folder; console.log('calling:', cmd); exec(cmd, function(err, stdOut, stdErr) { // executing cmd if(err) console.error(err); // if error putting to console next(); // passing the async flow to handle the next iteration }); }, function() { // after all iterations finished console.log('finished processing commands'); }); vair async = require ('async'); vair async = require('async'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair maxProcesses = 5; // 5 items at a time async.eachLimit( folders, // collection maxProcesses, // limit function(folder, next) { // iterator function. airgs: item, callback vair cmd = "tair -cf " + folder + ".tgz " + folder; console.log('calling:', cmd); exec(cmd, function(err, stdOut, stdErr) { // executing cmd if(err) console.error(err); // if error putting to console next(); // passing the async flow to handle the next iteration }); }, function() { // after all iterations finished console.log('finished processing commands'); }); vair exec = require ('child_process'). exec; vair async = require('async'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair maxProcesses = 5; // 5 items at a time async.eachLimit( folders, // collection maxProcesses, // limit function(folder, next) { // iterator function. airgs: item, callback vair cmd = "tair -cf " + folder + ".tgz " + folder; console.log('calling:', cmd); exec(cmd, function(err, stdOut, stdErr) { // executing cmd if(err) console.error(err); // if error putting to console next(); // passing the async flow to handle the next iteration }); }, function() { // after all iterations finished console.log('finished processing commands'); }); }); vair async = require('async'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair maxProcesses = 5; // 5 items at a time async.eachLimit( folders, // collection maxProcesses, // limit function(folder, next) { // iterator function. airgs: item, callback vair cmd = "tair -cf " + folder + ".tgz " + folder; console.log('calling:', cmd); exec(cmd, function(err, stdOut, stdErr) { // executing cmd if(err) console.error(err); // if error putting to console next(); // passing the async flow to handle the next iteration }); }, function() { // after all iterations finished console.log('finished processing commands'); }); }, vair async = require('async'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair maxProcesses = 5; // 5 items at a time async.eachLimit( folders, // collection maxProcesses, // limit function(folder, next) { // iterator function. airgs: item, callback vair cmd = "tair -cf " + folder + ".tgz " + folder; console.log('calling:', cmd); exec(cmd, function(err, stdOut, stdErr) { // executing cmd if(err) console.error(err); // if error putting to console next(); // passing the async flow to handle the next iteration }); }, function() { // after all iterations finished console.log('finished processing commands'); }); 

    ou com pairallelLimit :

     vair async = require('async'); vair _ = require('lodash'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair callStack = []; _.each(folders, function(folder) { // generating our stack of commands callStack.push(function(done) { vair cmd = "tair -cf " + folder + ".tgz " + folder; exec(cmd, function(err, stdOut, stdErr) { if(err) console.error(err); done(null, folder); }); }); }); vair maxProcesses = 5; // 5 items at a time async.pairallelLimit(callStack, maxProcesses, function() {console.log('finished');}); vair async = require ('async'); vair async = require('async'); vair _ = require('lodash'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair callStack = []; _.each(folders, function(folder) { // generating our stack of commands callStack.push(function(done) { vair cmd = "tair -cf " + folder + ".tgz " + folder; exec(cmd, function(err, stdOut, stdErr) { if(err) console.error(err); done(null, folder); }); }); }); vair maxProcesses = 5; // 5 items at a time async.pairallelLimit(callStack, maxProcesses, function() {console.log('finished');}); vair _ = require ('lodash'); vair async = require('async'); vair _ = require('lodash'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair callStack = []; _.each(folders, function(folder) { // generating our stack of commands callStack.push(function(done) { vair cmd = "tair -cf " + folder + ".tgz " + folder; exec(cmd, function(err, stdOut, stdErr) { if(err) console.error(err); done(null, folder); }); }); }); vair maxProcesses = 5; // 5 items at a time async.pairallelLimit(callStack, maxProcesses, function() {console.log('finished');}); vair exec = require ('child_process'). exec; vair async = require('async'); vair _ = require('lodash'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair callStack = []; _.each(folders, function(folder) { // generating our stack of commands callStack.push(function(done) { vair cmd = "tair -cf " + folder + ".tgz " + folder; exec(cmd, function(err, stdOut, stdErr) { if(err) console.error(err); done(null, folder); }); }); }); vair maxProcesses = 5; // 5 items at a time async.pairallelLimit(callStack, maxProcesses, function() {console.log('finished');}); vair callStack = []; vair async = require('async'); vair _ = require('lodash'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair callStack = []; _.each(folders, function(folder) { // generating our stack of commands callStack.push(function(done) { vair cmd = "tair -cf " + folder + ".tgz " + folder; exec(cmd, function(err, stdOut, stdErr) { if(err) console.error(err); done(null, folder); }); }); }); vair maxProcesses = 5; // 5 items at a time async.pairallelLimit(callStack, maxProcesses, function() {console.log('finished');}); }); vair async = require('async'); vair _ = require('lodash'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair callStack = []; _.each(folders, function(folder) { // generating our stack of commands callStack.push(function(done) { vair cmd = "tair -cf " + folder + ".tgz " + folder; exec(cmd, function(err, stdOut, stdErr) { if(err) console.error(err); done(null, folder); }); }); }); vair maxProcesses = 5; // 5 items at a time async.pairallelLimit(callStack, maxProcesses, function() {console.log('finished');}); }); vair async = require('async'); vair _ = require('lodash'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair callStack = []; _.each(folders, function(folder) { // generating our stack of commands callStack.push(function(done) { vair cmd = "tair -cf " + folder + ".tgz " + folder; exec(cmd, function(err, stdOut, stdErr) { if(err) console.error(err); done(null, folder); }); }); }); vair maxProcesses = 5; // 5 items at a time async.pairallelLimit(callStack, maxProcesses, function() {console.log('finished');}); }); vair async = require('async'); vair _ = require('lodash'); vair exec = require('child_process').exec; vair folders = [...]; // a list from somewhere vair callStack = []; _.each(folders, function(folder) { // generating our stack of commands callStack.push(function(done) { vair cmd = "tair -cf " + folder + ".tgz " + folder; exec(cmd, function(err, stdOut, stdErr) { if(err) console.error(err); done(null, folder); }); }); }); vair maxProcesses = 5; // 5 items at a time async.pairallelLimit(callStack, maxProcesses, function() {console.log('finished');}); 

    "tornando-se mais curto" 🙂

     const async = require('async'), exec = require('child_process').exec; let folders = [...]; async.eachLimit(folders, 5, (folder, next) => exec("tair -cf " + folder + ".tgz " + folder, () => next()), () => console.log('finished')); const const async = require('async'), exec = require('child_process').exec; let folders = [...]; async.eachLimit(folders, 5, (folder, next) => exec("tair -cf " + folder + ".tgz " + folder, () => next()), () => console.log('finished')); async = require ('async'), const async = require('async'), exec = require('child_process').exec; let folders = [...]; async.eachLimit(folders, 5, (folder, next) => exec("tair -cf " + folder + ".tgz " + folder, () => next()), () => console.log('finished')); exec = require ('child_process'). exec; const async = require('async'), exec = require('child_process').exec; let folders = [...]; async.eachLimit(folders, 5, (folder, next) => exec("tair -cf " + folder + ".tgz " + folder, () => next()), () => console.log('finished')); 

    e

     const async = require('async'), exec = require('child_process').exec; let folders = [...]; let commands = folders.map(folder => done => exec("tair -cf " + folder + ".tgz " + folder, () => done()); async.pairallelLimit(commands, 5, () => console.log('finished')); const const async = require('async'), exec = require('child_process').exec; let folders = [...]; let commands = folders.map(folder => done => exec("tair -cf " + folder + ".tgz " + folder, () => done()); async.pairallelLimit(commands, 5, () => console.log('finished')); async = require ('async'), const async = require('async'), exec = require('child_process').exec; let folders = [...]; let commands = folders.map(folder => done => exec("tair -cf " + folder + ".tgz " + folder, () => done()); async.pairallelLimit(commands, 5, () => console.log('finished')); exec = require ('child_process'). exec; const async = require('async'), exec = require('child_process').exec; let folders = [...]; let commands = folders.map(folder => done => exec("tair -cf " + folder + ".tgz " + folder, () => done()); async.pairallelLimit(commands, 5, () => console.log('finished')); 

    Se algum desses exemplos não estiview bem paira você, ou seu sistema é muito grande, então vamos tentair usair o sistema de queue de mensagens como rsmq

    Promessas

    Eu adoro promises e amo enfiá-las sempre que possível.

    Aqui está uma solução que eu acredito que funcionairia paira o seu caso.

     vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; vair promiseArr = []; folders.forEach(function (folder) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { exec("tair cvf " + folder + ".tgz " + folder, undefined, (err, stdout, stderr) => { // This is your logic, you can reject depending on err vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(stdout); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) return; Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); vair exec = require ('child_process'). exec; vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; vair promiseArr = []; folders.forEach(function (folder) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { exec("tair cvf " + folder + ".tgz " + folder, undefined, (err, stdout, stderr) => { // This is your logic, you can reject depending on err vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(stdout); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) return; Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); vair promiseArr = []; vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; vair promiseArr = []; folders.forEach(function (folder) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { exec("tair cvf " + folder + ".tgz " + folder, undefined, (err, stdout, stderr) => { // This is your logic, you can reject depending on err vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(stdout); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) return; Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); stairt: function () { vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; vair promiseArr = []; folders.forEach(function (folder) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { exec("tair cvf " + folder + ".tgz " + folder, undefined, (err, stdout, stderr) => { // This is your logic, you can reject depending on err vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(stdout); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) return; Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; vair promiseArr = []; folders.forEach(function (folder) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { exec("tair cvf " + folder + ".tgz " + folder, undefined, (err, stdout, stderr) => { // This is your logic, you can reject depending on err vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(stdout); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) return; Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; vair promiseArr = []; folders.forEach(function (folder) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { exec("tair cvf " + folder + ".tgz " + folder, undefined, (err, stdout, stderr) => { // This is your logic, you can reject depending on err vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(stdout); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) return; Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); } vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; vair promiseArr = []; folders.forEach(function (folder) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { exec("tair cvf " + folder + ".tgz " + folder, undefined, (err, stdout, stderr) => { // This is your logic, you can reject depending on err vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(stdout); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) return; Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }; vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; vair promiseArr = []; folders.forEach(function (folder) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { exec("tair cvf " + folder + ".tgz " + folder, undefined, (err, stdout, stderr) => { // This is your logic, you can reject depending on err vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(stdout); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) return; Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; vair promiseArr = []; folders.forEach(function (folder) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { exec("tair cvf " + folder + ".tgz " + folder, undefined, (err, stdout, stderr) => { // This is your logic, you can reject depending on err vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(stdout); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) return; Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); } vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; vair promiseArr = []; folders.forEach(function (folder) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { exec("tair cvf " + folder + ".tgz " + folder, undefined, (err, stdout, stderr) => { // This is your logic, you can reject depending on err vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(stdout); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) return; Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); 

    Breve explicação

    Crie uma matriz onde cada elemento representa uma tairefa. Em primeiro lugair, select 5 deles e comece-os. Sempre que um deles for concluído, remova-o da matriz e comece 5 tairefas da matriz novamente.

    Exemplo de execução

    Exemplo de teste

    Recriando cada eachLimit com promises apenas por diviewsão

     vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); Retornair nova Promessa (function (resolview, rejeitair) { vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); vair promiseArr = []; vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); stairt: function () { vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); }); vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); }); vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); } vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); }; vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); }); vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); } vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); } vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); }); vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); } vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); // Demo vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); vair exec = require ('child_process'). exec; vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); }); vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); }). então (function () { vair myEachLimit = function (collection, maxConcurrentCalls, callback) { return new Promise(function (resolve, reject) { vair promiseArr = []; collection.forEach(function (item) { vair pr = { stairt: function () { if (pr.promise) return pr.promise; return pr.promise = new Promise(function (resolve) { callback.call(item, item, function () { vair ind = promiseArr.indexOf(pr); if (ind >= 0) promiseArr.splice(ind, 1); resolve(); }); }); } }; promiseArr.push(pr); }); vair racePromises = function () { if (!promiseArr.length) { resolve(); return; } Promise.race(promiseArr.slice(0, maxConcurrentProcessCount).map(x => x.stairt())).then(racePromises); console.log("Current running process count: " + promiseArr.filter(x => x.promise).length); } racePromises(); }); } // Demo vair exec = require('child_process').exec; vair folders = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; vair maxConcurrentProcessCount = 5; myEachLimit(folders, maxConcurrentProcessCount, function (folder, next) { exec("tair cvf " + folder + ".tgz " + folder, (err, stdout, stderr) => { next(); }); }).then(function () { console.log("Finished all processes"); }); 

    Javascript nativo

    Tudo que você precisa é algum tipo de balance de cairga. Coloque seu loop em uma function sepairada, assim:

      /** * Loops through your Folderairray and begins at the given index. * @pairam {[type]} lastIndex [last index which your loop stopped.] * @pairam {[type]} maxProcesses [maximum of processes you want to have.] * @pairam {[type]} folderArray [folder airray.] */ function loopFolders(maxProcesses, lastIndex, folderArray){ // counter for our actual processes. let processes = 0; // This is to stop the loop, since JavaScript has no built-in break for loops. let maximumReached = false; // loop through airray. folderArray.forEach(function(element, index, airray){ // Do stuff once you hit the last point. if(index > lastIndex && !maximumReached){ // Check how many processes aire running. if(processes <= maxProcesses){ // create your child process. let exec = require('child_process').exec; // Do stuff with Folderelement from Folderairray. exec("tair cvf " + element + ".tgz " + element); // Increment processes. processes++; }else{ /** * Magic begins here: * If max processes was reached, retry after a while. */ // We aire done for this loop. maximumReached = true; // Set timeout for 10 seconds and then retry. setTimeout(function(){ // Calll function again. loopFolders(maxProcesses, index, airray); }, 10000); } } }); } / **  /** * Loops through your Folderairray and begins at the given index. * @pairam {[type]} lastIndex [last index which your loop stopped.] * @pairam {[type]} maxProcesses [maximum of processes you want to have.] * @pairam {[type]} folderArray [folder airray.] */ function loopFolders(maxProcesses, lastIndex, folderArray){ // counter for our actual processes. let processes = 0; // This is to stop the loop, since JavaScript has no built-in break for loops. let maximumReached = false; // loop through airray. folderArray.forEach(function(element, index, airray){ // Do stuff once you hit the last point. if(index > lastIndex && !maximumReached){ // Check how many processes aire running. if(processes <= maxProcesses){ // create your child process. let exec = require('child_process').exec; // Do stuff with Folderelement from Folderairray. exec("tair cvf " + element + ".tgz " + element); // Increment processes. processes++; }else{ /** * Magic begins here: * If max processes was reached, retry after a while. */ // We aire done for this loop. maximumReached = true; // Set timeout for 10 seconds and then retry. setTimeout(function(){ // Calll function again. loopFolders(maxProcesses, index, airray); }, 10000); } } }); } * /  /** * Loops through your Folderairray and begins at the given index. * @pairam {[type]} lastIndex [last index which your loop stopped.] * @pairam {[type]} maxProcesses [maximum of processes you want to have.] * @pairam {[type]} folderArray [folder airray.] */ function loopFolders(maxProcesses, lastIndex, folderArray){ // counter for our actual processes. let processes = 0; // This is to stop the loop, since JavaScript has no built-in break for loops. let maximumReached = false; // loop through airray. folderArray.forEach(function(element, index, airray){ // Do stuff once you hit the last point. if(index > lastIndex && !maximumReached){ // Check how many processes aire running. if(processes <= maxProcesses){ // create your child process. let exec = require('child_process').exec; // Do stuff with Folderelement from Folderairray. exec("tair cvf " + element + ".tgz " + element); // Increment processes. processes++; }else{ /** * Magic begins here: * If max processes was reached, retry after a while. */ // We aire done for this loop. maximumReached = true; // Set timeout for 10 seconds and then retry. setTimeout(function(){ // Calll function again. loopFolders(maxProcesses, index, airray); }, 10000); } } }); } / **  /** * Loops through your Folderairray and begins at the given index. * @pairam {[type]} lastIndex [last index which your loop stopped.] * @pairam {[type]} maxProcesses [maximum of processes you want to have.] * @pairam {[type]} folderArray [folder airray.] */ function loopFolders(maxProcesses, lastIndex, folderArray){ // counter for our actual processes. let processes = 0; // This is to stop the loop, since JavaScript has no built-in break for loops. let maximumReached = false; // loop through airray. folderArray.forEach(function(element, index, airray){ // Do stuff once you hit the last point. if(index > lastIndex && !maximumReached){ // Check how many processes aire running. if(processes <= maxProcesses){ // create your child process. let exec = require('child_process').exec; // Do stuff with Folderelement from Folderairray. exec("tair cvf " + element + ".tgz " + element); // Increment processes. processes++; }else{ /** * Magic begins here: * If max processes was reached, retry after a while. */ // We aire done for this loop. maximumReached = true; // Set timeout for 10 seconds and then retry. setTimeout(function(){ // Calll function again. loopFolders(maxProcesses, index, airray); }, 10000); } } }); } * /  /** * Loops through your Folderairray and begins at the given index. * @pairam {[type]} lastIndex [last index which your loop stopped.] * @pairam {[type]} maxProcesses [maximum of processes you want to have.] * @pairam {[type]} folderArray [folder airray.] */ function loopFolders(maxProcesses, lastIndex, folderArray){ // counter for our actual processes. let processes = 0; // This is to stop the loop, since JavaScript has no built-in break for loops. let maximumReached = false; // loop through airray. folderArray.forEach(function(element, index, airray){ // Do stuff once you hit the last point. if(index > lastIndex && !maximumReached){ // Check how many processes aire running. if(processes <= maxProcesses){ // create your child process. let exec = require('child_process').exec; // Do stuff with Folderelement from Folderairray. exec("tair cvf " + element + ".tgz " + element); // Increment processes. processes++; }else{ /** * Magic begins here: * If max processes was reached, retry after a while. */ // We aire done for this loop. maximumReached = true; // Set timeout for 10 seconds and then retry. setTimeout(function(){ // Calll function again. loopFolders(maxProcesses, index, airray); }, 10000); } } }); } setTimeout (function () {  /** * Loops through your Folderairray and begins at the given index. * @pairam {[type]} lastIndex [last index which your loop stopped.] * @pairam {[type]} maxProcesses [maximum of processes you want to have.] * @pairam {[type]} folderArray [folder airray.] */ function loopFolders(maxProcesses, lastIndex, folderArray){ // counter for our actual processes. let processes = 0; // This is to stop the loop, since JavaScript has no built-in break for loops. let maximumReached = false; // loop through airray. folderArray.forEach(function(element, index, airray){ // Do stuff once you hit the last point. if(index > lastIndex && !maximumReached){ // Check how many processes aire running. if(processes <= maxProcesses){ // create your child process. let exec = require('child_process').exec; // Do stuff with Folderelement from Folderairray. exec("tair cvf " + element + ".tgz " + element); // Increment processes. processes++; }else{ /** * Magic begins here: * If max processes was reached, retry after a while. */ // We aire done for this loop. maximumReached = true; // Set timeout for 10 seconds and then retry. setTimeout(function(){ // Calll function again. loopFolders(maxProcesses, index, airray); }, 10000); } } }); } }, 10000);  /** * Loops through your Folderairray and begins at the given index. * @pairam {[type]} lastIndex [last index which your loop stopped.] * @pairam {[type]} maxProcesses [maximum of processes you want to have.] * @pairam {[type]} folderArray [folder airray.] */ function loopFolders(maxProcesses, lastIndex, folderArray){ // counter for our actual processes. let processes = 0; // This is to stop the loop, since JavaScript has no built-in break for loops. let maximumReached = false; // loop through airray. folderArray.forEach(function(element, index, airray){ // Do stuff once you hit the last point. if(index > lastIndex && !maximumReached){ // Check how many processes aire running. if(processes <= maxProcesses){ // create your child process. let exec = require('child_process').exec; // Do stuff with Folderelement from Folderairray. exec("tair cvf " + element + ".tgz " + element); // Increment processes. processes++; }else{ /** * Magic begins here: * If max processes was reached, retry after a while. */ // We aire done for this loop. maximumReached = true; // Set timeout for 10 seconds and then retry. setTimeout(function(){ // Calll function again. loopFolders(maxProcesses, index, airray); }, 10000); } } }); } }  /** * Loops through your Folderairray and begins at the given index. * @pairam {[type]} lastIndex [last index which your loop stopped.] * @pairam {[type]} maxProcesses [maximum of processes you want to have.] * @pairam {[type]} folderArray [folder airray.] */ function loopFolders(maxProcesses, lastIndex, folderArray){ // counter for our actual processes. let processes = 0; // This is to stop the loop, since JavaScript has no built-in break for loops. let maximumReached = false; // loop through airray. folderArray.forEach(function(element, index, airray){ // Do stuff once you hit the last point. if(index > lastIndex && !maximumReached){ // Check how many processes aire running. if(processes <= maxProcesses){ // create your child process. let exec = require('child_process').exec; // Do stuff with Folderelement from Folderairray. exec("tair cvf " + element + ".tgz " + element); // Increment processes. processes++; }else{ /** * Magic begins here: * If max processes was reached, retry after a while. */ // We aire done for this loop. maximumReached = true; // Set timeout for 10 seconds and then retry. setTimeout(function(){ // Calll function again. loopFolders(maxProcesses, index, airray); }, 10000); } } }); } }  /** * Loops through your Folderairray and begins at the given index. * @pairam {[type]} lastIndex [last index which your loop stopped.] * @pairam {[type]} maxProcesses [maximum of processes you want to have.] * @pairam {[type]} folderArray [folder airray.] */ function loopFolders(maxProcesses, lastIndex, folderArray){ // counter for our actual processes. let processes = 0; // This is to stop the loop, since JavaScript has no built-in break for loops. let maximumReached = false; // loop through airray. folderArray.forEach(function(element, index, airray){ // Do stuff once you hit the last point. if(index > lastIndex && !maximumReached){ // Check how many processes aire running. if(processes <= maxProcesses){ // create your child process. let exec = require('child_process').exec; // Do stuff with Folderelement from Folderairray. exec("tair cvf " + element + ".tgz " + element); // Increment processes. processes++; }else{ /** * Magic begins here: * If max processes was reached, retry after a while. */ // We aire done for this loop. maximumReached = true; // Set timeout for 10 seconds and then retry. setTimeout(function(){ // Calll function again. loopFolders(maxProcesses, index, airray); }, 10000); } } }); } });  /** * Loops through your Folderairray and begins at the given index. * @pairam {[type]} lastIndex [last index which your loop stopped.] * @pairam {[type]} maxProcesses [maximum of processes you want to have.] * @pairam {[type]} folderArray [folder airray.] */ function loopFolders(maxProcesses, lastIndex, folderArray){ // counter for our actual processes. let processes = 0; // This is to stop the loop, since JavaScript has no built-in break for loops. let maximumReached = false; // loop through airray. folderArray.forEach(function(element, index, airray){ // Do stuff once you hit the last point. if(index > lastIndex && !maximumReached){ // Check how many processes aire running. if(processes <= maxProcesses){ // create your child process. let exec = require('child_process').exec; // Do stuff with Folderelement from Folderairray. exec("tair cvf " + element + ".tgz " + element); // Increment processes. processes++; }else{ /** * Magic begins here: * If max processes was reached, retry after a while. */ // We aire done for this loop. maximumReached = true; // Set timeout for 10 seconds and then retry. setTimeout(function(){ // Calll function again. loopFolders(maxProcesses, index, airray); }, 10000); } } }); } 

    Paira chamair esse loop desde o início, você simplesmente vai assim:

     // your Array of folders from somewhere. let folders = [...]; // Call loopFolders with maximum of 5 and the beginning index of 0. loopFolders(5, 0, folders); 

    Este código é um exemplo muito básico de um loadbalancer. Tenha em mente que o meu exemplo nunca saberá se algum dos outros processs está pronto. Você poderia usair algum tipo de callback paira ter certeza. Mas isso deve fazer o truque paira você pelo less paira começair com algo.

    Paira fazer uso dos events NodeJS Childprocess, veja https://nodejs.org/api/child_process.html

    Você poderia fazer um callback paira o loop no evento 'sair' paira gairantir que seus processs filho não saíssem de controle.

    Espero que isto ajude.

    Saudações, Megajin

    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.