一直在网上寻找答案,但没有找到任何结论。
我有一个节点应用程序(可能)需要发出大量的HTTP GET请求。
让我们说http://foo.com/bar允许一个' id'查询参数,我有大量的ID要处理(~1k),即
http://foo.com/bar?id=100
http://foo.com/bar?id=101
等
人们使用过哪些库可能最适合这项任务?
我想我正在寻找队列和连接池之间的东西:
设置:
过程:
X
个'工作人员的游泳池'已定义X
个并发工人)欢迎任何经验
答案 0 :(得分:0)
它实际上比我最初想的要简单得多,而且只需要Bluebird(我在这里解释一下,因为我的最终代码最终变得复杂得多):
var Promise = require('bluebird');
...
var allResults = [];
...
Promise.map(idList, (id) => {
// For each ID in idList, make a HTTP call
return http.get( ... url: 'http://foo.com/bar?id=' + id ... )
.then((httpResposne) => {
return allResults.push(httpResposne);
})
.catch((err) => {
var errMsg = 'ERROR: [' + err + ']';
console.log(errMsg + (err.stack ? '\n' + err.stack : ''));
});
}, { concurrency: 10 }) // Max of 10 concurrent HTTP calls at once
.then(() => {
// All requests are now complete, return all results
return res.json(allResults);
});