简体   繁体   English

如何从nodejs刷新子进程

[英]How can I flush a child process from nodejs

I invoke an executable C program from nodejs using spawn, but the C program seems not be flushed every time.我使用 spawn 从 nodejs 调用了一个可执行的 C 程序,但 C 程序似乎不是每次都被刷新。 Then, my node program can only output the child process' stdout when the buffer is full(4K) or the child process exit.然后,我的节点程序只能在缓冲区已满(4K)或子进程退出时输出子进程的标准输出。

To simply the scenario, the two parts code should be:简单来说,两部分代码应该是:

invoke.js调用.js

var spawn = require("child_process").spawn;

var ps = spawn("./echo");
ps.stdout.on("data", (data) => {
    console.log(`${data}`); 
});
process.on("SIGINT", ps.kill);

echo.c回声文件

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

static volatile int keep = 1;

void intHandler(int d){
    keep = 0;
}

int main(){
    signal(SIGINT, intHandler);
    int count = 0;
    while(keep) {
            printf("%d hello world!\r\n", ++count);
            sleep(1);
    }
}

How could I get the output from echo in the invoke.js synchronus?我怎样才能在 invoke.js 同步中从 echo 获得输出? One important thing is I cannot modify the echo.c, I only have binary一件重要的事情是我不能修改 echo.c,我只有二进制文件

我发现execa对此很有用。

You need to read the documentation about child processes/forks and how to pipe the child output to the parent process: https://nodejs.org/api/child_process.html#child_process_options_stdio您需要阅读有关子进程/分叉以及如何将子输出通过管道传输到父进程的文档: https : //nodejs.org/api/child_process.html#child_process_options_stdio

  • 'pipe' - equivalent to ['pipe', 'pipe', 'pipe'] (the default) 'pipe' - 相当于 ['pipe', 'pipe', 'pipe'] (默认)
  • 'ignore' - equivalent to ['ignore', 'ignore', 'ignore'] 'ignore' - 相当于 ['ignore', 'ignore', 'ignore']
  • 'inherit' -equivalent to [process.stdin, process.stdout, process.stderr] or [0,1,2] 'inherit' - 相当于 [process.stdin, process.stdout, process.stderr] 或 [0,1,2]

In your case, if you don't need to do something more complex with the output, you can get rid of the on.data event listener and just initialize the spawned process with the inherit option.在你的情况下,如果你不需要对输出做一些更复杂的事情,你可以摆脱on.data事件侦听器,只需使用继承选项初始化生成的进程。 I've modified the SIGINT event binding to be more clear that the ps.kill is invoked.我已经修改了 SIGINT 事件绑定,以便更清楚地说明调用了 ps.kill。

var spawn = require('child_process').spawn;

var ps = spawn('./echo', { stdio: 'inherit' });

process.on('SIGINT', function (){
  console.log('sigint received');
  ps.kill();
});

https://nodejs.org/dist/latest-v12.x/docs/api/process.html#process_a_note_on_process_i_o https://nodejs.org/dist/latest-v12.x/​​docs/api/process.html#process_a_note_on_process_i_o

There are different between Windows and POSIX. Windows 和 POSIX 之间存在差异。 If child_process is spawned on Windows, only TTY type stdout work asynchronously.如果 child_process 在 Windows 上产生,则只有 TTY 类型的 stdout 异步工作。 inherit option make child_process asynchronously, because process.stdout is TTY type. inherit选项使 child_process 异步,因为process.stdout是 TTY 类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM