Skip to content

Instantly share code, notes, and snippets.

@michiel
Created February 10, 2011 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michiel/820893 to your computer and use it in GitHub Desktop.
Save michiel/820893 to your computer and use it in GitHub Desktop.
Run 10000 async calls through a pipeline'd sequencer
//
// Example #3 : sequence 10000 async calls in a pipeline, with max 500 open
//
function pipeline(arr, maxOpenCalls, finalCallback) {
var openCalls = 0;
function callback() {
if (--openCalls < maxOpenCalls) {
launch();
}
}
function launch() {
if (arr.length != 0) {
do {
openCalls++;
arr.shift()(callback);
} while (openCalls < maxOpenCalls);
} else if ((arr.length == 0) && (openCalls == 0)){
console.log("Finished all calls");
finalCallback();
} else {
console.log("Too many in flight, waiting");
}
}
var initial = (maxOpenCalls > arr.length) ? maxOpenCalls : arr.length;
for (var i=0; i<initial; i++) {
launch();
}
}
//
// Build an array with 10000 sequencable async calls
//
var asyncCalls = [];
for (var i=0;i<10000;i++) {
(function(no) {
asyncCalls.push(
function(callback) {
console.log("Call #" + no);
setTimeout(callback, 200); // Simulate async
}
);
})(i);
}
pipeline(asyncCalls, 500, function() { console.log("All done"); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment