Javascript - 如何控制并行访问网络的承诺数量 [英] Javascript - how to control how many promises access network in parallel

查看:10
本文介绍了Javascript - 如何控制并行访问网络的承诺数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一系列并行访问网络的承诺,但有时,当我的应用程序全速运行时,由于许多承诺访问网络,我的网络速度变慢,我想知道我如何可以控制并行接入网络的数量.这是代码示例:

in my application, I have and array of promises that access network in parallel, but some times, when my app is running full speed, my network slows down, due to many promises accessing network, I would like to know how I could control how many access network in parallel. This is a sample of code:

var ids = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 6: 56, 7: 7, 8: 8, 5:6 }; // this is random 
Promise.all( Object.keys(ids).map(function(dp){
  return new Promise(function(resolve, reject){

       http.post({url: addr, form: { data: dp }}, function(err, res, body){
        if (err){
            reject(err)
        }            
        resolve(body.xx);
      });

  });
})).then(function(data){
       http.post({url: hostAddress, form: { data: data.x }}, function(err, res, body){
        ......
        resolve(body.xx);
      });    
});
  });
}))

有很多网络.我会很棒,我只能同时允许 2 或 3 个.感谢您的帮助.

There are a lot of networking. I would be great I could allow only 2 or 3 at the same time. Thanks for your help.

推荐答案

您可以使用 Bluebird 的 .map() 有一个并发选项来控制同时进行的请求数:

You can use Bluebird's .map() which has a concurrency option to control how many requests are in-flight at the same time:

const Promise = require('bluebird');
const http = Promise.promisifyAll(require('http');

var ids = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 6: 56, 7: 7, 8: 8, 5:6 }; // this is random 

Promise.map(Object.keys(ids).map(function(dp){
    return  http.post({url: addr, form: { data: dp }).then(function(body) {
        return body.xx;
    });
}), {concurrency: 2}).then(function(results) {
    // process results here
});

仅供参考,我不明白您想用第二个 http.post() 做什么,因为您在 data 时引用了 data.x 是一个数组.我认为该代码有点伪代码,无法说明您真正想用第二个 http.post() 做什么.

FYI, I didn't understand what you were trying to do with your second http.post() because you where referencing data.x when data is an array. I think the code is a bit too much pseudo-code to illustrate what you were really trying to do with that second http.post().

否则,您可以编写自己的并发控制代码,首先启动 N 个请求,然后每次完成一个请求,然后启动另一个,直到您无事可做.下面是手动编写并发控制的示例:

Otherwise, you can code your own concurrency control where you fire up N requests initially and then each time one finishes, you fire up another until you have no more to do. Here's an example of coding the concurrency control manually:

一次发出 1,000,000 个请求 100 个

或者,您可以像这样自己编写:

Or, you could write it yourself like this:

const http = require('http');

function httpPost(options) {
    return new Promise(function(resolve, reject) {
        http.post(options, function(err, res, body) {
            if (err) {
                reject(err);
            } else {
                resolve(body);
            }
        });
    });
}

// takes an array of items and a function that returns a promise
function mapConcurrent(items, maxConcurrent, fn) {
    let index = 0;
    let inFlightCntr = 0;
    let doneCntr = 0;
    let results = new Array(items.length);
    let stop = false;

    return new Promise(function(resolve, reject) {

        function runNext() {
            let i = index;
            ++inFlightCntr;
            fn(items[index], index++).then(function(val) {
                ++doneCntr;
                --inFlightCntr;
                results[i] = val;
                run();
            }, function(err) {
                // set flag so we don't launch any more requests
                stop = true;
                reject(err);
            });
        }

        function run() {
            // launch as many as we're allowed to
            while (!stop && inflightCntr < maxConcurrent && index < items.length) {
                runNext();
            }
            // if all are done, then resolve parent promise with results
            if (doneCntr === items.length) {
                resolve(results);
            }
        }

        run();
    });
}

var ids = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 6: 56, 7: 7, 8: 8, 5:6 }; // this is random 


mapConcurrent(Object.keys(ids), 2, function(item, index) {
    return httpPost({url: addr, form: {data: item}}).then(function(body) {
        return body.xxx;
    });
}).then(function(results) {
    // array of results here
}, function(err) {
    // error here    
});

这篇关于Javascript - 如何控制并行访问网络的承诺数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆