Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created January 28, 2015 02:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheckmann/569831d5f4377770610d to your computer and use it in GitHub Desktop.
Save aheckmann/569831d5f4377770610d to your computer and use it in GitHub Desktop.
configurable parallelism
/**
* example of configurable parallelism
*/
// silly counter
var i = 0;
// configurable amount of work to run in parallel
var max = 1;
// counts number of active jobs
var running = 0;
// manages amount of running work
function work() {
while (running < max) {
++running
doStuff();
}
}
// some worker logic
function doStuff() {
console.log('I am running %d in parallel (%d)', running, ++i);
// pretend async call doing i/o or whatever
async(function(err){
if (err) {
// probably stop all work but depends
return;
}
// after the async work is finished, see if we need to do more work
--running;
work();
}
}
work();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment