如何限制角$ Q承诺并发? [英] How can I limit angular $q promise concurrency?

查看:197
本文介绍了如何限制角$ Q承诺并发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何做类似 $ q.all ,但限制了多少承诺并发执行?

How do I do something like $q.all but limiting how many promises are executed concurrently?

我的问题是,就像如何限制Q承诺并发?

我想不会超过5进程产生在时间

I want no more than 5 process spawned at a time

对于其他问题,接受的答案是承诺写了一个库包裹着的问:
但我在一个解决方案感兴趣的专门角的 $ Q ,而不是问:

The accepted answer for that other question is a library written for promise wrapped to work with Q. But I'm interested specifically in a solution for Angular's $q rather than for Q.

背景:这个问题正在得到解决:结果
我有一大堆的文件在2个步骤下载:一)获取URL二)下载​​文件搜索
,所以当使用简单的承诺与 $ q.all 浏览器限制多少文件可以同时触发检索了所有的下载,只有
N 立即发生,如: 6 在Chrome中,其余的则推迟。 <子>(见最大并行http连接在浏览器?的结果
问题是,网址有到期,因此由当时的浏览器执行的N + 1 文件下载,网址是不再有效。结果
所以,我想要做的事,比如 throttled.all(6,承诺),而不是 $ q.all(承诺)

Background: The problem being solved:
I have a bunch of files to download in 2 steps: a) Get URL b) download file.
The browser limits how many files can be retrieved concurrently, so when the straightforward use of promises with $q.all fires off all the downloads, only
N happen right away, e.g. 6 in Chrome, while the rest are delayed. (see Max parallel http connections in a browser?)
Problem is that the URLs have expiry, so by the time the browser executes the N+1th file download, the URL is no longer valid.
So I want to do something like throttled.all(6, promises) rather than $q.all(promise)

推荐答案

下面是一个替代的解决方案将运行并发块。它并不像其他的答案一样干净,但它是更接近你在找什么。

Here is an alternate solution that will run concurrent Chunks. It is not quite as clean as the other answer, but it is closer to what you are looking for.

我离开用于验证一些标记。

self.list [...];

self.MakeCall = function(theList, chunkSize,methodTracker){
    //Set default chunkSize
    chunkSize = (chunkSize)?chunkSize:1;

    //Slice list using previously defined list 
    // so multiple instances can be runuse same list
    var processList = self.list.slice(0,chunkSize);
    self.list = self.list.slice(chunkSize);

    return $q.all(processList).then(function(result) {
      //If holdList is empty you are finished
      if(self.list <= 0) {
        console.debug("method last: " + methodTracker);
        return result;
      }
      else {
        //More items make recursive call to process next set
        console.debug("method: " + methodTracker);
        return self.MakeCall(self.list, chunkSize,methodTracker);
      }
    });
  };
  //Initialize Recursive call
  self.MakeCall(self.list,1,1).then(function() {
    console.warn('end 1');
  });
  //Initialize Second call that will run asynchronous with the first
  // this can be initialized as many times as you want concurrent threads
  self.MakeCall(self.list,1,2).then(function() {
    console.warn('end 2');
  });

我选择以另一种答案。我认为两者是不同的不够,我不想更改其他

这篇关于如何限制角$ Q承诺并发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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