nodejs 并行回调设计模式 [英] nodejs parallel callback design pattern
问题描述
我正在努力寻找一种好的模式来执行一堆并行任务.
I'm trying to find a good pattern to execute a bunch of parallel tasks.
让我定义一些任务来举例说明.任务 a, b, c, d, e, f, g
执行为 a(function(er, ra){//task a 返回,ra 是结果})
,b
到 g
Let me define some task to exemplify. Tasks a, b, c, d, e, f, g
execute as a(function(er, ra){//task a returned, ra is result})
, so do b
to g
还有一些任务是在某个任务完成后才执行的,我们称它们为ab, bc, abc, bd, bcd, af, fg
,意思是当a
和 b
已经返回 ab(ra, rb)
应该立即执行,当 b
和 c
> 返回,bc(rb, rc)
应该立即执行,如果a
, b
, c
全部返回,abc(ra, rb, rc)
应该被执行.
There are also some tasks that should be execute after some task is done, let's call them ab, bc, abc, bd, bcd, af, fg
, means when a
and b
has returned ab(ra, rb)
should be executed at once, and when b
and c
returned, bc(rb, rc)
should be executed at once, and if a
, b
, c
all returned, abc(ra, rb, rc)
should be executed.
对于最简单的情况,如果只有a
和b
,我可以这样做:
For the simplest case, if there is only a
and b
, I can do something like this:
(function(cb){
var count = 2, _ra, _rb;
function update(){if(--count == 0) cb(null, _ra, _rb)}
a(function(er, ra){_ra = ra; update()});
b(function(er, ra){_rb = rb; update()});
})(function(er, ra, rb){
ab(ra, rb);
});
如你所见,a
和 b
并行执行,当两者都完成后,ab(ra, rb)
执行.
As you can see, a
and b
execute in parallel, and when both are done, ab(ra, rb)
execute.
但是我怎样才能为大量并行任务做更多的事情?
But how can I do more things for a lot of parallel tasks?
推荐答案
你真正想要的是一个延迟模式,尽管像 futures.
What you actually want is a deferred pattern though like futures.
function defer(f) {
// create a promise.
var promise = Futures.promise();
f(function(err, data) {
if (err) {
// break it
promise.smash(err);
} else {
// fulfill it
promise.fulfill(data);
}
});
return promise;
}
var da = defer(a), db = defer(b), dc = defer(c), dd = defer(d), de = defer(e), df = defer(f), dg = defer(g);
// when a and b are fulfilled then call ab
// ab takes one parameter [ra, rb]
Futures.join(da, db).when(ab);
Futures.join(db, dc).when(bc);
// abc takes one parameter [ra, rb, rc]
Futures.join(da, db, dc).when(abc);
Futures.join(db, dd).when(bd);
Futures.join(db, dc, dd).when(bcd);
Futures.join(da, df).when(af);
// where's e ?
Futures.join(df,dg).when(fg);
Futures.join(da,db,dc,dd,de,df,dg).fail(function() {
console.log(":(");
});
这篇关于nodejs 并行回调设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!