当前位置:网站首页>一次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);
});
}
边栏推荐
- 一键搭建博客:如何使用WordPress插件搭建专属博客
- How to use "copy – link" to accelerate docker to build and optimize cache
- AI model risk assessment Part 2: core content
- 如何使用 grep 跨多行查找模式匹配
- Qt 之自定义界面(实现无边框、可移动)
- 面试官培训课件(非常实用的企业内训课件)
- [image processing] image skeleton extraction based on central axis transformation with matlab code
- PaddleLite 编译以及代码跑通复盘
- 精通音视频开发是真的可以为所欲为
- 593. 有效的正方形
猜你喜欢

面试官培训课件(非常实用的企业内训课件)

ES6 arrow function this points to

Function comparison between report control FastReport and stimulus soft

如何开始为您的 Kubernetes 应用程序编写 Helm 图表

ECCV 2022 | SSP: 自支持匹配的小样本任务新思想

2022 latest WiFi master applet independent version 3.0.8

【Untitled】

Niuke net brush questions

MyCat中间件高可用、读写分离、分片、主从切换、ER分片

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
随机推荐
Why should kubernetes be used in development environments
为什么应该在开发环境中使用 Kubernetes
『知识集锦』一文搞懂mysql索引!!(建议收藏)
Qt 之自定义界面(实现无边框、可移动)
如何使用“COPY –link”加速 Docker 构建和优化缓存
GBase8s Informix Dodker 高可用集群自恢复集群启动命令oninitdb的设计与实现
Paddlelite compilation and code running through the disk
AMH6.X升级到AMH7.0后,登录后台提示MySQL连接出错怎么解决?
幸运抽奖系统带后台源码
[SwiftUI 开发] @State @Binding @ObservedObject @EnvironmentObject
LeetCode_416_分割等和子集
INVALID_ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
2022年企业直播行业发展洞察
Spark efficient data analysis 01. Establishment of idea development environment
QML(二):设置自定义窗体
How to use grep to display file names and line numbers before matching lines
PyQt5快速开发与实战 6.6 QFormLayout(表单布局) && 6.7 嵌套布局 && 6.8 QSplitter
Design and implementation of gbase8s Informix dodker high availability cluster self recovery cluster startup command oninitdb
就这?TypeScript其实并不难!(建议收藏)
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29