danyo
danyo

Reputation: 11

node.js pipe a stream out of a (forked) child_process

I am trying to communicate with a (forked) child-process from in node.js.

The background is that I want to run a stream in another thread.

I have a nodejs parent process that starts up another nodejs child process. The child process executes some logic and then returns output to the parent.

Parent File Code:

const stream = require('stream');
const Writable = stream.Writable;
const fork = require("child_process").fork;

class Logger extends Writable {
  constructor() {
    super({ objectMode: true });
  }

  _write(chunk, encoding, callBack) {
    console.log(`${Date.now()} - (payload:${chunk})`);
    callBack(null);
  }
}

const writeStream = new Logger();

const computedStream = fork('child.js', [], { silent: true });
computedStream.stdout
  .pipe(writeStream);

Child file code:

const stream = require('stream');
const Readable = stream.Readable;

class RandomNumberGenerator extends Readable {
  constructor() {
    super({ objectMode: true });
    this._count = 10;
    this._counter = 0;
  }

  _read() {
    if (this._counter === this._count) {
      return this.push(null);
    }
    const random = Math.random();
    this.push(random)

    this._counter++;
  }
}

const readStream = new RandomNumberGenerator();

readStream.pipe(process.stdout);

The above code prints out nothing and i am waiting for sth. like this

1546139560637 - (payload:0.05907150771370184)
1546139560642 - (payload:0.395942443503438)
1546139560642 - (payload:0.7873116185362699)
...

Upvotes: 1

Views: 855

Answers (1)

ForgetfulFellow
ForgetfulFellow

Reputation: 2632

My hunch is that you cannot just console.log in another thread and expect it to output on the master thread. You'll need to send the information back and then console.log it on the master thread.

Given that the results are properly inside process.stdout

Child.js

// After all processing has finished on the child thread
process.send({ info: process.stdout });

Parent File Code

const computedStream = fork('child.js', [], { silent: true });
computedStream.on('message', (message) => {
   console.log(`stdout of child processes is: ${message.info}`);
});

More information can be found here - https://itnext.io/multi-threading-and-multi-process-in-node-js-ffa5bb5cde98

Upvotes: 1

Related Questions