当前位置:网站首页>一次node文件操作过多排查过程总结
一次node文件操作过多排查过程总结
2022-07-29 11:31:00 【poetry】
前端进阶之旅:https://interview2.poetries.top 博客:https://blog.poetries.top 公众号/小程序:「前端进阶之旅」 每天分享技术干货,学前端不迷路 作者:程序员poetry
大家好,我是poetry。最近在优化公司内部的脚手架,遇到一个问题,Error: EMFILE, too many open files也就是nodejs打开文件过多会导致错误,一次次排查,最后找到了一个有效的方法,总结记录一下
当我尝试去操作大量文件的时候
for(var i=0; i<200000; i++) {
fq.readFile('./somefile.txt', {encoding: 'utf8'}, function(err, somefile) {
console.log("data from somefile.txt without crashing!", somefile);
});
}
以上导致
Error: EMFILE: too many open files错误。我不必关闭文件,因为显然可以fs.readFile对文件进行操作并为我关闭文件。我在Mac OS上,我的ulimit设置为8192
$ ulimit -a
-t: cpu time (seconds) unlimited
-f: file size (blocks) unlimited
-d: data seg size (kbytes) unlimited
-s: stack size (kbytes) 8192
-c: core file size (blocks) 0
-v: address space (kbytes) unlimited
-l: locked-in-memory size (kbytes) unlimited
-u: processes 1392
-n: file descriptors 256

可以通过修改系统配置,但是不太推荐
$ echo kern.maxfiles=65536 | sudo tee -a /etc/sysctl.conf
$ echo kern.maxfilesperproc=65536 | sudo tee -a /etc/sysctl.conf
$ sudo sysctl -w kern.maxfiles=65536
$ sudo sysctl -w kern.maxfilesperproc=65536
$ ulimit -n 65536
由于 node.js的异步特性,因此会发生此错误。进程试图打开的文件超出允许的数量,因此会产生错误。可以通过创建打开 文件队列来解决此问题,以使它们永远不会超过限制,以下是一些可以为您完成此操作的库:
我们可以使用文件队列来限制每次打开的文件数量
Instantiate Filequeue with a maximum number of files to be opened at once (default is 200)
how to use
var FileQueue = require('filequeue');
var fq = new FileQueue(100);
// additional instances will attempt to use the same instance (and therefore the same maxfiles)
var FileQueue2 = require('filequeue');
var fq2 = new FileQueue2(100);
console.log(fq === fq2); // => true
// you can force a new instance of filequeue with the `newQueue` parameter
var fq3 = new FileQueue(100, true);
console.log(fq === fq3); // => false
filequeue支持以下方法
readFile
writeFile
readdir
rename
symlink
mkdir
stat
exists
createReadStream
createWriteStream
使用filequeue就可以正常运行了
var FileQueue = require('filequeue');
var fq = new FileQueue(100); // 限制每次打开的文件数量
for(var i=0; i<200000; i++) {
fq.readFile('./demo.txt', {encoding: 'utf8'}, function(err, somefile) {
console.log("data from somefile.txt without crashing!", somefile);
});
}
边栏推荐
- ECCV 2022 | SSP: 自支持匹配的小样本任务新思想
- GBase8s Informix Dodker 高可用集群自恢复集群启动命令oninitdb的设计与实现
- 2022最新 wifi大师小程序独立版3.0.8
- Leetcode bit operation
- 基于flask写的一个小商城mall项目
- INVALID_ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
- 『面试知识集锦100篇』1.面试技巧篇丨HR的小心思,你真的懂吗?
- Paddlelite compilation and code running through the disk
- Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
- 从零开始Blazor Server(3)--添加cookie授权
猜你喜欢

共建共享数字世界的根:阿里云打造全面的云原生开源生态

How to use "copy – link" to accelerate docker to build and optimize cache

谷歌“消灭” Cookie 计划延至 2024 年

如何在匹配行之前使用 grep 显示文件名和行号

QML(一):自定义圆角按钮的处理
![[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code](/img/c1/f962f1c1d9f75732157d49a5d1d0d6.png)
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code

Meituan and hungry were interviewed by Hangzhou supervisors to implement the responsibility of food safety management and prohibit malicious competition

std::vector 拷贝、追加、嵌套访问

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

Sunwenlong, Secretary General of the open atom open source foundation, worked together to expand open source
随机推荐
Applied practical skills of deep reinforcement learning
PHP basics uses arrays to save data
"Knowledge Collection" article to understand mysql index!!(recommended collection)
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
AMH6.X升级到AMH7.0后,登录后台提示MySQL连接出错怎么解决?
【Untitled】
游戏合作伙伴专题:BreederDAO 与《王国联盟》结成联盟
ASN.1接口描述语言详解
Xiaoxiao authorization system V5.0 happy version
HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
Niuke net brush questions
Deep understanding of c # delegate into the fast lanes
IPV6基础
"100 Interview Knowledge Collections" 1. Interview Skills丨Do you really understand HR's careful thinking?
GBase8s Informix Dodker 高可用集群自恢复集群启动命令oninitdb的设计与实现
On CompareTo method in string
Out-of-the-box problem-solving thinking, putting a "rearview mirror" on the unconscious life
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
Zhou Hongyi: 360 is the largest secure big data company in the world