Skip to content

Instantly share code, notes, and snippets.

@anon767
Last active September 20, 2017 07:13
Show Gist options
  • Save anon767/119fe4e911e1ba9325fa3d1d171772b4 to your computer and use it in GitHub Desktop.
Save anon767/119fe4e911e1ba9325fa3d1d171772b4 to your computer and use it in GitHub Desktop.
Class to handle many concurrent open file handles in nodejs
//Script in case you get following error in your project
/*
Error: EMFILE: too many open files, open '%file%'
at Error (native)
*/
var fs = require('fs');
var fsHandler = function(callBack){
var queue = [];
var semaphore = 0;
var addFile = function(filename){
queue.push(filename);
}
this.checkQueue = function(){
semaphore--;
if(queue.length >0)
this.checkFile(queue.shift())
}
this.readFile = function(filename){
var self = this;
fs.readFile(filename, 'utf8', function read(err, data) {
if (err) {
throw err;
}
callBack(data);
self.checkQueue();
});
}
this.checkFile = function(filename){
if(semaphore >= 50 ){
queue.push(filename);
}else{
this.readFile(filename);
semaphore++;
}
}
}
function processFile(content) {
console.log(content);
}
var fsHandler = new fsHandler(processFile);
for(var i = 0; i <100000;i++){
fsHandler.checkFile("./read.txt");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment