Created
January 28, 2015 02:25
-
-
Save aheckmann/569831d5f4377770610d to your computer and use it in GitHub Desktop.
configurable parallelism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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