-
-
Save piclez/702219 to your computer and use it in GitHub Desktop.
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
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