Ram
Ram

Reputation: 432

Can't get piped stream from child process

In parent.js

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

in child 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)
  }
});

My problem i have no errors, and child process in not exit.

But child.stdout.on("data") is not triggered, i don't know what happens in background.

Why i can't get data from child ?? what am i missing, can anyone help please ??

Upvotes: 1

Views: 97

Answers (1)

uminder
uminder

Reputation: 26150

In parent.js, you should change child.stdout.on into child.on in order to receive what the child process is sending.

Also you should inverse the logic in parent.js

  1. child.on
  2. child.send

Please have a look at https://nodejs.org/api/child_process.html#child_process_subprocess_send_message_sendhandle_options_callback

Upvotes: 2

Related Questions