当前位置:网站首页>一次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);
});
}
边栏推荐
- 北京大学公开课重磅来袭!欢迎走进「AI for Science」课堂
- SkiaSharp 之 WPF 自绘 弹动小球(案例版)
- Design and implementation of gbase8s Informix dodker high availability cluster self recovery cluster startup command oninitdb
- The heavyweight foundation awarded platinum, gold and silver donors
- GBase8s核心数据备份
- 【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
- PaddleLite 编译以及代码跑通复盘
- golang 实现文件上传下载
- Niuke net brush questions
- mysql单行,多行子查询
猜你喜欢
MyCat中间件高可用、读写分离、分片、主从切换、ER分片
迁徙数据平台简单介绍
【图像处理】基于中轴变换实现图像骨架提取附matlab代码
Out-of-the-box problem-solving thinking, putting a "rearview mirror" on the unconscious life
Sunwenlong, Secretary General of the open atom open source foundation, worked together to expand open source
即学即用的问题解决思维,给无意识的生活装上“后视镜”
如何开始为您的 Kubernetes 应用程序编写 Helm 图表
Learning with Recoverable Forgetting阅读心得
如何在匹配行之前使用 grep 显示文件名和行号
How to use "copy – link" to accelerate docker to build and optimize cache
随机推荐
3.认识和操作一下mysql的基本命令
INVALID_ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
GBase8s Informix Dodker 高可用集群自恢复集群启动命令oninitdb的设计与实现
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主
2022年企业直播行业发展洞察
建议收藏丨sql行转列的一千种写法!!
精通音视频开发是真的可以为所欲为
Hugo NexT V4 介绍
Use anyio instead of asyncio
开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览
LeetCode_ 1049_ Weight of the last stone II
从零开始Blazor Server(3)--添加cookie授权
AI全流程开发难题破解之钥
One click blog building: how to use WordPress plug-in to build a dedicated blog
Alibaba architects spent a year sorting out the "Lucene advanced document", and you are also a big factory employee!
Insights into the development of the enterprise live broadcast industry in 2022
基于flask实现的mall商城---用户模块
北京大学公开课重磅来袭!欢迎走进「AI for Science」课堂
Is this it?TypeScript actually not difficult!(recommended collection)
2022 latest WiFi master applet independent version 3.0.8