【问题标题】:NodeJS: Append file inside a callback functionNodeJS:在回调函数中追加文件
【发布时间】:2020-03-25 04:36:20
【问题描述】:

我有一个 Electron JS 应用程序,它启动了一个我用 C++ 编写的独立子进程(非阻塞)(二进制应用程序)。

我希望将 C++ 标准输出 [STDOUT]流式传输到 NodeJS 并将其记录到日志文本文件中。

我已经在 NodeJS 子进程文档 (link here) 中看到了使用 spawn()fork()exec()execFile() 之间的区别。就我而言,我需要使用spawn() 方法。

这是我的 Electron JS / NodeJS 代码:

const path = require('path');
const fs = require("fs");
const cp = require("child_process");

[... another stuffs ...]

// Start a child process:
const child = cp.spawn(my_binary, ["param1", "param2", "param3"], { detached: true });
child.unref();

// STDOUT listener:
child.stdout.setEncoding('utf8');
child.stdout.on("data", (data) => {

    // Set the log file path:
    let filePath = path.join(__dirname, "..", "logs", logFilename);

    // Try to append: I GOT A REFRESH PAGE LOOP HERE:
    fs.appendFile(filePath, data, (err) => {
        if (err) {
            console.error("[STDOUT]", err);
        } else {
            // Get the C++ STDOUT output data:
            console.log(data);
        }
    });
})

这段代码实际上工作,并将 C++ 输出记录在 log_timestamp.txt 文件中。

我的问题

当我调用这个 NodeJS 文件时,ElectronJS Web 界面和浏览器控制台在无限刷新循环中每秒刷新很多次。 因此,每次 Electron JS 刷新页面时,NodeJS 都会启动再次一个新的子进程。 最后,我在几秒钟内收到了很多log_timestamp.txt

如果我只删除 fs.appendFile() 并将其替换为 console.log(data)我只得到了我想要的控制台日志输出一次,没有页面刷新或异常行为。 示例:

const path = require('path');
const fs = require("fs");
const cp = require("child_process");

[... another stuffs ...]

// Start a child process:
const child = cp.spawn(my_binary, ["param1", "param2", "param3"], { detached: true });
child.unref();

// STDOUT listener:
child.stdout.setEncoding('utf8');
child.stdout.on("data", (data) => {

    // Set the log file path:
    let filePath = path.join(__dirname, "..", "logs", logFilename);

    // Get the C++ STDOUT output data:
    console.log(data);
})

我做错了什么? 如何修复第一个源代码,以便在文本文件中正确记录 C++ 输出?

我的系统是:

  • 电子 7.1.2
  • 节点 12.13.0
  • Windows 10 x64 和 Ubuntu 18 x64

更新 1

按照@Jonas 的建议,我尝试了这个替代代码,但我得到了同样的异常行为。

// Start a child process:
const child = cp.spawn(bin, ["param1", "param2", "param3"], { detached: true });
child.unref();

// STDOUT listener:
child.stdout.setEncoding('utf8');
child.stdout.on("data", (data) => {
    console.log(data);
})

let filePath = path.join(__dirname, "..", "logs", logFilename);
child.stdout.pipe(fs.createWriteStream(filePath));

【问题讨论】:

    标签: javascript node.js asynchronous electron fs


    【解决方案1】:

    流的意义在于,您可以将它们通过管道连接在一起,让引擎处理所有其余的:

     child.stdout.pipe(fs.createWriteStream(/*...*/));
    

    我认为您的主要问题是appendFile 必须打开文件、锁定文件、写入文件、解锁文件,并且这发生在每个数据块上。这会减慢写入速度,并且由于您没有正确处理背压,内存消耗开始增加,并可能导致您看到的延迟。

    【讨论】:

    • 感谢@Jonas Wilms 的快速回答。我做了这个改变,但我得到了同样的异常行为。您还有其他想法吗?
    • 我是 ElectronJS 和 NodeJS 编程的新手……所以我不知道如何查找/使用分析器?不正常的是:当我尝试将 C++ 输出附加到文本文件时,ElectronJS 每秒刷新前端很多次。因此,每次它发生时,NodeJS 都会启动一个新的子进程,并且我会得到很多日志文件。我想创建一个日志文件。为此,我的 Electron Web 界面无法一直刷新。您还有其他建议吗?
    猜你喜欢
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2018-04-29
    • 2016-12-09
    • 2019-04-03
    • 1970-01-01
    • 2017-10-20
    • 2013-05-08
    相关资源
    最近更新 更多