简体   繁体   English

child_process 的节点 JS 生成

[英]Node JS spawn of child_process

I'm spawning a command to play an audio stream with mpg123 , which works fine.我正在生成一个命令来播放带有mpg123的音频 stream ,效果很好。 I expose a method to kill the process as well which also works well, but I can't figure out how to get output from the said command.我也公开了一种杀死进程的方法,它也很有效,但我不知道如何从上述命令中获取 output。

No events are fired on the child processes stdout , going by the docs it should work - what could be the issue here?在子进程stdout上没有触发任何事件,按照它应该工作的文档进行 - 这里可能是什么问题?

One solution I've found is to set stdio property of spawn to inherit , but then spawn returns null, which is not ideal as I can't create a handle to kill it later.我发现的一种解决方案是将spawnstdio属性设置为inherit ,但随后spawn返回 null,这并不理想,因为我无法创建句柄来稍后杀死它。

I'm running this on a Pi Zero with Node 10.24.我在节点 10.24 的 Pi Zero 上运行它。

export const streamFactory = (cb: (data: string) => void) => {
  try {
    const st = spawn("mpg123", [configManager.getStreamUrl()]);

    st.stdout.on("data", (data) => {
      console.log(data);
      cb(data);
    });

    return {
      close: () => {
        st.kill("SIGINT");
      },
    };
  } catch (e) {
    console.log(e);
  }
};

For some reason, mpg123 outputs its data on stderr , not stdout .出于某种原因, mpg123将其数据输出到stderr ,而不是stdout Making the switch works like a charm, go figure.使开关像魅力一样工作,go 图。

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

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