nodejs并行回调设计模式 [英] nodejs parallel callback design pattern
问题描述
我试图找到一个好的模式来执行一堆并行任务。
让我定义一些任务来例证。任务 a,b,c,d,e,f,g
执行为 a(function(er,ra){// task a returned, ra is result})
,因此 b
至 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)
$ b
对于最简单的情况,如果只有 a
和 b
做这样的事情:
(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
code> b 并行执行,当两者都执行时, ab(ra,rb)
$ b
但是我怎么能为很多并行任务做更多的事情呢?
code> function defer(f){
//创建一个promise。
var promise = Futures.promise();
f(function(err,data){
if(err){
// break it
promise.smash(err);
} else {
//完成它
promise.fulfill(data);
}
});
return promise;
}
var da = defer(a),db = defer(b),dc = defer(c),dd = defer(d) ),dg = defer(g);
//当a和b满足时调用ab
// ab需要一个参数[ra,rb]
Futures.join(da,db).when );
Futures.join(db,dc).when(bc);
// abc需要一个参数[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(:();
}
I'm trying to find a good pattern to execute a bunch of parallel tasks.
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
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.
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);
});
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?
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屋!