Q中并发限制的承诺 - 节点 [英] Concurrency limit in Q promises - node

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

问题描述

有没有什么方法来限制​​用品质承诺库?承诺的并发

Is there any method to limit concurrency of promises using Q promises library?

此问题是有点儿关系到如何限制Q承诺并发?

This question is kinda related to How can I limit Q promise concurrency?

但问题是,我试图做这样的事情:

but the problem is that I'm trying to do something like this:

for (var i = 0; i <= 1000; i++) {
  return Q.all([ task1(i), task2(i) ]); // <-- limit this to 2 at a time.
}

真正的使用情况是:

The real use case is:


  1. 从数据库获取的帖子

  2. 循环每一个岗位在DB像 posts.forEach(功能(POST){}

  3. 对于每一个岗位做独立写作,TASK2,TASK3(检索社会柜台,检索评论数等)

  4. 在DB保存新的岗位数据。

但问题是该节点正在执行的所有帖子所有任务的同时,就像在同一时间要求的Facebook的喜欢伯爵500职位。

But the problem is that node is executing all tasks for all posts at the same time, like asking facebook for the "likes count" for 500 posts at the same time.

如何限制 Q.all()所以一次只2个职位正在执行他们的任务?或者有什么其他可能的解决方案可以在这里申请?

How i can limit Q.all() so only 2 posts at a time are executing their tasks? Or what other possible solutions can apply here?

请注意:大多数的任务(如果不是全部)依赖于要求库

Note: Most of the tasks (if not all) rely on request library

推荐答案

由于丹,他的答案以及他的帮助下它与我的code集成,它可以使用自己的要点以及这样的snipplet:

Thanks to Dan, his answer and his help to integrate it with my code, it can be done using his gist and a snipplet like this:

var qlimit = require('../libs/qlimit');

var test = function(id) {
  console.log('Running ' + id);
  return Q.nfcall(request, 'some dummy url which takes some time to process, for example a php file with sleep(5)').spread(function(response, body) {
    console.log('Response ' + id);
    return body;
  });
}

test = qlimit.limitConcurrency(test, 1);

var data = [0, 1, 2];

data.forEach(function(id) {
  console.log('Starting item ' + id);
  Q.all([ test(id) ]);
});

这样,你得到的东西,如:

This way you get something like:


  • 启动项0

  • 启动项1

  • 启动项目2

  • 运行0

  • 响应0

  • 运行1

  • 响应1

  • 运行2

  • 响应2

其中明确是一次1请求。

Which clearly is 1 request at a time.

这是我在缺少实施整点是,你需要重新申报,而不是在它里面使用limitConcurrency开始循环之前的功能。

The whole point that i was missing in the implementation is that you need to re-declare the function using limitConcurrency BEFORE starting the loop, not inside it.

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

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