Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: EMFILE: too many open files

Using nw.js, I am just trying to save images in an array of img elements with different random names.

But having a few errors, is something wrong with my code?

for (i = 0; i < imgs.length; i++) {
 request(imgs[i].getAttribute('src')).on('error', function(err) {
  throw err
 }).pipe(fs.createWriteStream('data/imgs/' + randomString))
}

imgs[] is an array of 100-500 html img element, but I am receiving

Error: EMFILE: too many open files, open *<directory>*

And another error:

"Uncaught Error: socket hang up"

Although it saves some images, some of them are corrupted, and it creates too many images than there actually is.

like image 807
CRQ Avatar asked Jun 14 '15 14:06

CRQ


1 Answers

Use graceful-fs module, which is a drop-in replacement for the fs module. It is a wrapper around the native fs module. Quoting the docs of graceful-fs module,

Queues up open and readdir calls, and retries them once something closes if there is an EMFILE error from too many file descriptors.

Since it exposes the same APIs as the native fs module, you can use it just like the normal fs module.

// use just like fs
var fs = require('graceful-fs');

Note: This library was created by, Isaac Z. Schlueter, one of the core developers of the Node.js.

like image 144
thefourtheye Avatar answered Oct 21 '22 07:10

thefourtheye