use*_*286 8 javascript concurrency node.js q
有没有办法限制在节点js中一次执行的并发Q promise的数量?
我正在构建一个web scrapper,它必须请求和解析更多的3000多个页面而且没有限制我做的一些请求没有按时响应,因此连接休息并且所需的响应(html代码)变得不可用.
为了反击行动,我发现限制我的问题消失的请求数量.
我尝试过以下方法,但无济于事:
我需要请求一个url数组,一次只执行1个请求,并且当数组中的所有url都已完成时,然后将结果返回到数组中.
function processWebsite() {
//computed by this stage
urls = [u1,u2,u3,u4,l5,u6,u7,u8,u9];
var promises = throttle(urls,1,myfunction);
// myfunction returns a Q promise and takes a considerable
// amount of time to resolve (approximately 2-5 minutes)
Q.all(promises).then(function(results){
//work with the results of the promises array
});
}
Run Code Online (Sandbox Code Playgroud)
then()您可以在块中请求新的 url
myFunction(urls[0]).then(function(result) {
myFunction(urls[1]).then(function(result) {
myFunction(urls[2]).then(function(result) {
...
});
});
});
Run Code Online (Sandbox Code Playgroud)
当然,这将是其动态行为。一旦承诺得到解决,我将维护一个队列并出列单个网址。然后提出另一个请求。也许还有一个将 url 与结果相关联的哈希对象。
第二次拍摄:
var urls = ...;
var limit = ...;
var dequeue = function() {
return an array containing up to limit
};
var myFunction = function(dequeue) {
var urls = dequeue();
$q.all(process urls);
};
myFunction(dequeue).then(function(result) {
myFunction(dequeue).then(function(result) {
myFunction(dequeue).then(function(result) {
...
});
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1444 次 |
| 最近记录: |