无法从子进程中获取管道流

时间:2019-07-07 10:00:49

标签: javascript node.js stream

在parent.js中

child = fork(filePath,[],{silent : true});
child.send({
        msgtype: "path",
        path: path,
        options: options,
      });
child.stdout.on("data",(data)=>{
     console.log(data);
})

在子process.js中

let createQueue = require("queue-async");
let readdirp = require("readdirp");

process.on("message", function(msg) {
  let files = "";
  if (msg && msg.msgtype === "path") {
    let pathname = msg.path;
    files = readdirp(pathname);
    process.send({
      msg: "detials",
      details: msg
    });
    files.on("error", e => {
      process.send({
        msg: "killMe",
        details: null
      });
    });
    files.on("end", () => {
      process.send({
        msg: "killMe",
        details: null
      });
    });
    files.pipe(process.stdout)
  }
});

我的问题我没有错误,子进程也没有退出。

但是child.stdout.on("data")没有触发,我不知道后台会发生什么。

为什么我不能从孩子那里得到数据?我想念的是什么,任何人都可以帮忙吗??

1 个答案:

答案 0 :(得分:2)

parent.js中,应将child.stdout.on更改为child.on,以便接收子进程发送的内容。

另外,您应该颠倒parent.js

中的逻辑
  1. child.on
  2. child.send

请看看https://nodejs.org/api/child_process.html#child_process_subprocess_send_message_sendhandle_options_callback