如何使用async.parallelLimit最大化(并行)运行进程的数量? [英] how to use async.parallelLimit to maximize the amount of (paralle) running processes?

查看:111
本文介绍了如何使用async.parallelLimit最大化(并行)运行进程的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用async.parallelLimit为并行运行的进程设置一个限制?我使用以下非常简单的代码来测试其工作方式.

Is it possible to set a Limit to parallel running processes with async.parallelLimit ? I used following very simple code to test how it works.

var async = require("async");
var i = 0;

function write() {
    i++;
    console.log("Done", i);
}

async.parallelLimit([
    function(callback) {
        write();
        callback();
    }
], 10, function() {
    console.log("finish");
});

当然,我得到的一切都是这样:

Of course everything what I got back was this:

Done 1
finish

就我而言,我经常调用一个函数,但是我只希望它同时运行5次.其他呼叫应排队.(是的,我了解async.queue,但这不是我想要的这个问题).

In my case I have a function wich is called very often, but I only want it to run only 5 times simultaneously. The other calls should be queued. (Yes I know about async.queue but, this is not what I want in this question).

所以现在的问题是,使用async.parallelLimit是否可能?

So now the question is, is this possible with async.parallelLimit?

//

我实际上有类似的东西:

I actually have something similar to this:

for(var i = 0; i < somthing.length; i++) { //i > 100  for example
    write();
}

并且由于node.js的不同步性,这可能同时运行100次.但是在这种情况下,shell如何限制并行运行的进程?

And 'cause of the non synchronicity of node.js this could run 100 times at the same time. But how shell I limit the parallel running processes in this case?

推荐答案

答案很简短;是的.这正是asyncParallelLimit所做的.

Very short answer; Yes. That's exactly what asyncParallelLimit does.

在您的情况下,您仅将一个函数传递给parallelLimit.这就是为什么它只被调用一次的原因.如果要多次传递具有相同功能的数组,则执行该数组的次数将与您将其放入数组的次数相同.

In your case, you are passing only one function to parallelLimit. That's why it only get's called once. If you were to pass an array with this same function many times, it will get executed as many times as you put it in the array.

请注意,您的示例函数实际上并未异步执行任何工作.因此,此示例函数将始终按顺序执行.如果您具有异步工作功能,例如网络请求或文件I/O,则会并行执行.

Please note that your example function doesn't actually do any work asynchronously. As such, this example function will always get executed in series. If you have a function that does async work, for example a network request or file i/o, it will get executed in parallel.

异步工作负载的更好的示例功能是:

A better example-function for a async workload would be:

function(callback) {
    setTimeout(function(){
        callback();
    }, 200);
}

这篇关于如何使用async.parallelLimit最大化(并行)运行进程的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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