有没有一种方法可以同时使用Bluebird的Promise.each? [英] Is there a way to use Bluebird's Promise.each concurrently?

查看:68
本文介绍了有没有一种方法可以同时使用Bluebird的Promise.each?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bluebird有一个很好的函数,叫做Promise.map,它使您可以传递一个额外的参数来表示并发操作的数量.

Bluebird has a nice function called Promise.map that lets you pass in an extra argument for the amount of concurrent operations.

例如

yield Promise.map arrayOfThings, coroutine (thing) ->
  newThing = yield thing.operate()
  database.set newThing
, concurrency: 500

但是,Promise.map将为所有arrayOfThings的内存保留一个database.set newThing返回的数组.我宁愿不要将所有这些存储在内存中,因为它会使我的服务器瘫痪.理想情况下,我想将Promise.map替换为Promise.each,以便它不将返回的值存储在内存中.不幸的是,这很慢,因为Promise.each不是并发的.

However, Promise.map will keep an array of whatever database.set newThing returns in memory for all of arrayOfThings. I'd rather not store all of that in memory as it bogs down my server. Optimally, I would want to replace Promise.map with Promise.each so it doesn't store the returned values in memory. Unfortunately this is super slow because Promise.each is not concurrent.

有什么办法可以更改我的代码以使其那样工作?

Is there any way I can change my code to make it work like that?

推荐答案

首先,目前Promise.each实际上并未分配数组.对此有一个未解决的问题-我被分配了,我想道歉-我不在开发人员面前,并且已经出国了.我会尽快解决.

First of all, at the moment Promise.each doesn't actually not allocate the array. There is an open issue for this - I'm assigned and I'd like to apologize - I'm not in front of a dev box and have been abroad. I'll try to fix this soon.

第二个-不.目前没有此类功能.创建Promise.each的目的是为了精确地顺序运行程序.拉取请求可能很有趣,在PromiseArray之上实现它应该不会太难.我们只是之前从未真正看到过用例.

Second of all - no. There is no such functionality at the moment. Promise.each was created in order to precisely run things sequentially. A pull request might be entertained and it shouldn't be too hard to implement on top of PromiseArray. We just haven't really seen the use case before.

与此同时,您可以使用Promise.map.

Meanwhile you can use Promise.map.

这篇关于有没有一种方法可以同时使用Bluebird的Promise.each?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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