【问题标题】:What's the different between Async.queue and Promise.map?Async.queue 和 Promise.map 有什么区别?
【发布时间】:2019-05-31 14:23:24
【问题描述】:

我尝试在 ExpressJS 中对我的 api 进行压力测试并处理多个请求,我使用了 Promise.all,然后使用了带有并发选项的 Async.queue。

  1. 承诺:

    export const myapi = async (args1, args2) => {
    console.log('args:', args1, args2);
    
    let testing_queue = [];
    testing_queue.push(new Promise(async (resolve, reject) => {
            let result = await doAComplexQuery(args1, args2); // SELECT... JOIN...
            if (!result || result.length <= 0)
                reject(new Error('Cannot find anything!'));
            resolve(result);
        }
    ));
    return await Bluebird.map(testing_queue, async item => {
        return item;
    }, {concurrency: 4});    };
    
  2. Async.queue: (https://www.npmjs.com/package/async)

    export const myapi = async (args1, args2) => {
    console.log('args:', args1, args2);
    
    let testing_queue = Async.queue(function (task, callback) {
        console.log('task', task);
        callback();
    }, 4);
    testing_queue.push(async function () {
            let result = await doAComplexQuery(args1, args2); // SELECT... JOIN...
            if (!result || result.length <= 0)
                throw new Error('Cannot find anything!');
            return result;
        }
    );};
    

并尽量提出请求:

const response = async function () {
    return await Axios.post('http://localhost:3000/my-api', {
        "args1": "0a0759eb",
        "args2": "b9142db8"
    }, {}
    ).then(result => {
        return result.data;
    }).catch(error => {
        console.log(error.message);
    });
};

for (var i = 0; i < 10000; i++) {
    response();
}

然后运行。 #1 方式返回许多 ResourceTimeout 或 Socket 挂断响应。同时,#2 为所有请求返回成功响应,运行速度更快。

那么在这种情况下 Async.queue 更好吗?

【问题讨论】:

标签: express


【解决方案1】:

我认为如果你提高你的 promise.map 的并发限制,它可以帮助提高速度。

【讨论】:

    猜你喜欢
    • 2019-07-29
    • 2010-10-02
    • 2011-12-12
    • 2010-09-16
    • 2012-03-14
    • 2012-02-06
    • 2011-02-25
    • 2011-11-22
    • 2015-03-26
    相关资源
    最近更新 更多