Sam*_*ami 5 javascript c++ fifo ulimit node.js
我有一个 node.js 进程,它需要从不同的其他进程提供的多个命名管道中读取作为 IPC 方法。
在打开并创建来自四个以上 fifos 的读取流后,我意识到 fs 似乎不再能够打开 fifos 并且只是挂在那里。
考虑到可以同时打开数千个文件而不会出现问题(例如在以下脚本中替换mkfifo为touch),这个数字似乎有点低。
我在 MacOS 10.13 上使用 node.js v10.1.0 和在 Ubuntu 16.04 上使用 node.js v8.9.3 进行了测试,结果相同。
错误的脚本
以及显示此行为的脚本:
var fs = require("fs");
var net = require("net");
var child_process = require('child_process');
var uuid = function() {
for (var i = 0, str = ""; i < 32; i++) {
var number = Math.floor(Math.random() * 16);
str += number.toString(16);
}
return str;
}
function setupNamedPipe(cb) {
var id = uuid();
var fifoPath = "/tmp/tmpfifo/" + id;
child_process.exec("mkfifo " + fifoPath, function(error, stdout, stderr) {
if (error) {
return;
}
fs.open(fifoPath, 'r+', function(error, fd) {
if (error) {
return;
}
var stream = fs.createReadStream(null, {
fd
});
stream.on('data', function(data) {
console.log("FIFO data", data.toString());
});
stream.on("close", function(){
console.log("close");
});
stream.on("error", function(error){
console.log("error", error);
});
console.log("OK");
cb();
});
});
}
var i = 0;
function loop() {
++i;
console.log("Open ", i);
setupNamedPipe(loop);
}
child_process.exec("mkdir -p /tmp/tmpfifo/", function(error, stdout, stderr) {
if (error) {
return;
}
loop();
});
Run Code Online (Sandbox Code Playgroud)
这个剧本在他身后不干净,别忘了 rm -r /tmp/tmpfifo
注意,这个问题的以下部分与我已经尝试回答的问题有关,但可能不是它的核心
这个脚本有两个有趣的事实
echo hello > fifo)节点能够再打开一个 FIFO,但不再从我们写入的那个接收调试信息
然后我尝试验证这是否与某些操作系统限制有关,例如打开的文件描述符数量。
ulimit -aMac 上的输出是
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 256
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 1418
virtual memory (kbytes, -v) unlimited
Run Code Online (Sandbox Code Playgroud)
没有任何东西指向 4 的某个限制。
C++ 暂定
然后我尝试用 C++ 编写一个类似的脚本。在 C++ 中,脚本成功打开了一百个 fifos。
请注意,这两种实现之间存在一些差异。在 C++ 中,
#include <string>
#include <cstring>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
int main(int argc, char** argv)
{
for (int i=0; i < 100; i++){
std::string filePath = "/tmp/tmpfifo/" + std::to_string(i);
auto hehe = open(filePath.c_str(), O_RDWR);
std::cout << filePath << " " << hehe << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
作为旁注,需要在执行脚本之前创建 fifos,例如
for i in $(seq 0 100); do mkfifo /tmp/tmpfifo/$i; done
潜在的 Node.js 相关问题
经过一番搜索,它似乎也与 Node.js Github 上的那个问题有关:
https://github.com/nodejs/node/issues/1941。
但是人们似乎在抱怨相反的行为(fs.open() 抛出 EMFILE 错误而不是默默挂起......)
正如你所看到的,我试图在很多方向上进行搜索,所有这些都让我想到了我的问题:
你知道什么可能导致这种行为吗?
谢谢
于是我在Node.js Github上问了这个问题,https://github.com/nodejs/node/issues/23220
从解决方案:
目前处理 FIFO 有点棘手。
open()默认情况下,系统调用在 FIFO 上阻塞,直到管道的另一侧也已打开。由于 Node.js 使用线程池进行文件系统操作,因此在open()调用未完成的情况下打开多个管道会耗尽该线程池。解决方案是在非阻塞模式下打开文件,但这有一个困难,即其他
fs调用不是在考虑非阻塞文件描述符的情况下构建的;net.Socket然而。因此,解决方案将如下所示:
Run Code Online (Sandbox Code Playgroud)fs.open('path/to/fifo/', fs.constants.O_RDONLY | fs.constants.O_NONBLOCK, (err, fd) => { // Handle err const pipe = new net.Socket({ fd }); // Now `pipe` is a stream that can be used for reading from the FIFO. });
| 归档时间: |
|
| 查看次数: |
961 次 |
| 最近记录: |