Skip to content

Instantly share code, notes, and snippets.

@kevinswiber
Created April 16, 2013 10:31
Show Gist options
  • Save kevinswiber/5394922 to your computer and use it in GitHub Desktop.
Save kevinswiber/5394922 to your computer and use it in GitHub Desktop.
A comparison of pipeworks and async. The premise: 1. Start a pipeline. 2. Delegate to workers passed into the execution context. 3. Pause until the workers are done. 4. Continue executing the pipeline.
var pipeworks = require('pipeworks');
var worker1 = pipeworks()
.fit(function(context, next) {
console.log('in worker 1');
next(context);
});
var worker2 = pipeworks()
.fit(function(context, next) {
console.log('in worker 2');
next(context);
});
var worker3 = pipeworks()
.fit(function(context, next) {
console.log('in worker 3');
next(context);
});
var main = pipeworks()
.fit(function(context, next) {
console.log('starting');
context.count = 0;
context.workers.forEach(function(worker) {
worker.siphon(context, next);
});
})
.fit(function(context, next) {
context.count++;
console.log('# of workers finished:', context.count);
if (context.count === context.workers.length) {
next(context);
}
})
.fit(function(context, next) {
console.log('done-zo!');
next(context);
});
main.flow({ workers: [worker1, worker2, worker3] });
/*
Output:
starting
in worker 1
# of workers finished: 1
in worker 2
# of workers finished: 2
in worker 3
# of workers finished: 3
done-zo
*/
var async = require('async');
var worker1 = function(context, next) {
console.log('in worker 1');
next(context);
};
var worker2 = function(context, next) {
console.log('in worker 2');
next(context);
};
var worker3 = function(context, next) {
console.log('in worker 3');
next(context);
};
// wrap workers in a counter
var counter = function(worker, context) {
return function(next) {
worker(context, function() {
context.count++;
console.log('# of workers finished:', context.count);
next(null, context);
});
};
};
// use the flow function to kick-off the pipeline
var flow = function(context, pipeline) {
var start = function(next) {
next(null, context);
};
pipeline.unshift(start);
async.waterfall(pipeline);
};
var main = [
function(context, next) {
console.log('starting');
context.count = 0;
for(var i = 0, len = context.workers.length; i < len; i++) {
context.workers[i] = counter(context.workers[i], context);
}
async.parallel(context.workers, next);
},
function(context, next) {
console.log('done-zo');
next(null, context);
}
];
flow({ workers: [worker1, worker2, worker3] }, main);
/*
Output:
starting
in worker 1
# of workers finished: 1
in worker 2
# of workers finished: 2
in worker 3
# of workers finished: 3
done-zo
*/
@kevinswiber
Copy link
Author

Note that the Pipeworks workers are full-featured pipelines that may have multiple pipes added to them. For the async example, I'm just using functions as the workers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment