Skip to content

Instantly share code, notes, and snippets.

@dominictarr
Created September 26, 2010 08:01
Show Gist options
  • Save dominictarr/597716 to your computer and use it in GitHub Desktop.
Save dominictarr/597716 to your computer and use it in GitHub Desktop.
run a function with a limited amount of concurrency.
var sys = require('sys'),
exec = require('child_process').exec;
// fs = require('fs');
// ecsv = require('ecsv')
function Queue () {
var waiting = [];
this.max = -1;
this.current = 0;
//run checks if we're under the max processors, and runs if there is room.
this.run = function () {
if (this.max < 0 || this.current < this.max) {
this.current = this.current + 1;
p = waiting.pop()
if ('function' === typeof p){
p();
}
}
}
//start starts a process or puts it in the queue
this.start = function (f) {
sys.print(this.current);
waiting.push(f);
this.run();
}
//the callback has to call finish so that the Queue knows it can run something else.
this.finish = function() {
sys.print('!');
this.current = this.current - 1;
this.run();
}
}
//ecsv.each('../pub/data/crime_incident_data.csv', function(crime) {
q = new Queue ();
q.max = 250;
for (i = 0 ; i < 1000; i ++) {
q.start (function (){
exec('wc -c "foo"', function(err, stdout, stderr) {
console.log('woot');
q.finish();
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment