node and Error: EMFILE, too many open files

前端 未结 18 1815
终归单人心
终归单人心 2020-11-28 17:58

For some days I have searched for a working solution to an error

Error: EMFILE, too many open files

It seems that many people have the same proble

18条回答
  •  悲哀的现实
    2020-11-28 18:23

    cwait is a general solution for limiting concurrent executions of any functions that return promises.

    In your case the code could be something like:

    var Promise = require('bluebird');
    var cwait = require('cwait');
    
    // Allow max. 10 concurrent file reads.
    var queue = new cwait.TaskQueue(Promise, 10);
    var read = queue.wrap(Promise.promisify(batchingReadFile));
    
    Promise.map(files, function(filename) {
        console.log(filename);
        return(read(filename));
    })
    

提交回复
热议问题