节点生成标准延迟数据 [英] Node spawn stdout.on data delay
问题描述
我正在检查Linux上USB驱动器的拆卸.我只是使用child_process.spawn监视命令行过程的输出.但是由于某种原因,直到打印了20行,孩子的stdout数据事件才会发出,这使得它无法检测到卸下的驱动器.多次卸下驱动器后,它终于可以了.但这显然不会.
I am checking for USB drive removal on linux. I am simply monitoring the output of a command line process with child_process.spawn. But for some reason the child's stdout data event doesn't emit until like 20 lines have been printed, which makes it unable to detect a removed drive. After removing the drive many times, it does finally go. But obviously that won't do.
原件:
var udevmonitor = require("child_process").spawn("udevadm", ["monitor", "--udev"]);
udevmonitor.stdout.on("data", function(data) {
return console.log(data.toString());
});
非常简单.所以我想这是管道节点在内部使用的问题.因此,我想不使用管道,而是使用简单的直通流.那可以解决问题,并给我实时输出.该代码是:
Pretty simple. So I figure it's an issue with the piping node is using internally. So instead of using the pipe, I figure I'll just use a simple passthrough stream. That could solve the problem and give me real-time output. That code is:
var stdout = new require('stream').PassThrough();
require("child_process").spawn("udevadm", ["monitor", "--udev"], { stdio: ['pipe', stdout, 'pipe'] });
stdout.on("data", function(data) {
console.log(data.toString());
});
但是这给了我一个错误: child_process.js:922抛出新的TypeError('stdio流的值不正确:'+ stdio);
But that gives me an error: child_process.js:922 throw new TypeError('Incorrect value for stdio stream: ' + stdio);
文档说您可以传递流.我看不到我做错了什么,单步执行child_process源也无济于事.
The documentation says you can pass a stream in. I don't see what I'm doing wrong and stepping through the child_process source didn't help.
有人可以帮忙吗?如果您使用的是Linux,则可以自己运行.运行代码并插入USB驱动器.也许您可以在另一个终端上运行命令"udevadm monitor --udev"以查看会发生什么.删除并重新插入几次,最终将打印出节点.
Can someone help? You can run this yourself, provided you're on Linux. Run the code and insert a USB drive. Perhaps you can run the command 'udevadm monitor --udev' in another terminal to see what happens. Remove and reinsert a few times and eventually node will print out.
推荐答案
mscdex,我爱你.将spawn命令更改为
mscdex, I love you. Changing the spawn command to
spawn("stdbuf", ["-oL", "-eL", "udevadm", "monitor", "--udev"]);
成功了.非常感谢您的帮助!
Did the trick. I really appreciate your help!
这篇关于节点生成标准延迟数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!