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

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

问题描述

如何执行 $ q.all ,但是限制同时执行多少个promise?



我的问题就像如何限制Q promise并发?



< blockquote>

我想要一次产生不超过5个进程


是为承诺包装的图书馆,可以使用 Q
但是我特别感兴趣的是Angular的 $ q 而不是 Q 的解决方案。 p>

背景:要解决的问题:

可以通过两个步骤下载:a)获取URL b)下载文件。

浏览器限制可以同时检索多少个文件,因此在直接使用promise时 $ q。所有触发所有下载,仅
N 立即发生,例如 6 在Chrome中,而其余的延迟。 (请参阅浏览器中的最大并行HTTP连接数

问题是,网址已过期,因此在浏览器执行 N + 1 th 文件下载时,网址不再有效。

所以我想做 throttled.all(6,promises)而不是 $ q.all(promise)

解决方案

Promises是不相关的(像你在页面上更新一些元素,你不在乎它是渐进的,而不是在一个whoosh)我创建了一个简单的TaskQueue服务。它不会等待所有的Promise被解决,而是它将处理所有添加的Promises / Functions / Tasks,并获得最大值。并发配置的限制值。



由于我只发现这个和一些其他StackOverflows不帮助我的问题,我有。所以我现在给我回应社区我猜。希望它帮助某人。



它使用现代JS的东西,如const和lambda表达式,但你可以简单地让它从一个预编译器如babel编译,如果你只是想'



https:// gist。 github.com/Blackskyliner/8b1bafed326044fa4f8b1ba2627d1117



它只是简单地处理它的队列后任务,只是匿名函数,返回promise或值,添加。它将遵循服务上的一个可配置的maxConcurrentTasks变量。



如果任务返回一个返回promise的promise等等,它将总是使用队列中的初始'slot'。因此,在整个任务承诺链解决(或拒绝)后,它将为其他添加的任务释放slot。


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

My question is just like How can I limit Q promise concurrency?

I want no more than 5 process spawned at a time

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.

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)

解决方案

If the resolving of 'all' Promises is irrelevant (like you are updating some Elements on the page and you don't care if it's progressive and not in one 'whoosh') I have created a simple TaskQueue Service. It will not wait for all Promises to be resolved but rather it will process all added Promises/Functions/Tasks it gets and with a max. concurrency of a configured limit value.

As I only found this and some other StackOverflows not helping with my problem I had. So I now give something back to the community I guess. Hope it helps somebody.

It uses modern JS stuff like const and lambda expressions, but you may simply let it compile down from a precompiler like babel if you just want the 'old' stuff.

https://gist.github.com/Blackskyliner/8b1bafed326044fa4f8b1ba2627d1117

It just simply processes its queue after Tasks, which are just anonymous functions, returning a promise or value, are added. It will adhere to a configurable 'maxConcurrentTasks' variable on the service.

If the Task returns a promise which returns a promise and so on it will always use the initial 'slot' within the queue. So it will free the 'slot' for an other added Task after the whole Task Promise chain is resolved (or rejected).

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

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