function read(url) {
fs.readdir(url, function (err, files) {
files.forEach(function (file) {
var filePath = url + "/" + file;
fs.stat(filePath, function (err, stats) {
if (stats.isDirectory()) {
read(filePath);
} else {
var rl = readline.createInterface({
input: fs.createReadStream(filePath),
terminal: false
});
var fileType = file.split('_');
var jsonss = [];
rl.on('line', function (line) {
if (line != null && line != "") {
jsonss.push(JSON.parse(line));
}
}).on('close', function () {
var jsondata = { filename: file, data: jsonss };
if (fileType[fileType.length - 1] == 'dup') {
mongodao.qcdupModel.create(jsondata, function (err, res) {
if (err) {
console.log(err);
} else {
count++;
console.log('save qcdupmodel ok' + count);
console.info(res);
}
});
} else if (fileType[fileType.length - 1] == 'nodup') {
mongodao.qcnodupModel.create(jsondata, function (err, res) {
if (err) {
console.log(err);
} else {
count++;
console.log('save qcnodumpmodel ok' + count);
console.info(res);
}
});
} else {
mongodao.qcModel.create(jsondata, function (err, res) {
if (err) {
console.log(err);
} else {
count++;
console.log('save qcmodel ok' + count);
console.info(res);
}
});
}
});
console.info("===================================")
}
})
})
});
}
如果只有几个文件,这样是没问题的。但是文件一多起来的话,就会
我该怎么解决这个问题?
这个问题很有可能是系统本身的限制造成的,为了避免同时打开太多文件造成系统卡顿,一般都会有打开文件数量的限制。
解决方法主要有两种,一种是修改系统打开文件的数量上限 默认设置:
ulimit -n 10480
,另一种较为常见的方法是 使用更加友好的第三方库,比如 https://github.com/jprichards... 和 https://github.com/isaacs/nod...