Bob
Bob

Reputation: 14654

NodeJS multiprocessing

I am trying to write a script that creates multiple sub processes giving each process different environment variables, then it reports the subprocess outputs.

I can do something that runs everything then prints the messages as follows

const child_process = require("child_process");
const NUM_WORKERS = 5

for(let i = 0; i < NUM_WORKERS; ++i){
  child_process.exec(`node -e 'console.log(process.env.MY_VAR)'`, {
    env: {
      MY_VAR: i
    }
  }, (error, stdout, stderr) => {
    if(error)console.log(error)
    if(stdout){
      for(const row of stdout.split('\n')){
        console.log(`[stdout#${i}] ${row}`)
      }
    }
    if(stderr){
      if(stderr){
        for(const row of stderr.split('\n')){
          console.log(`[stderr#${i}] ${row}`)
        }
      }
    }
  })
}

How to get a version of this that print output messages as they are produced by the child processes instead of printing everything when the child processes ends?

Upvotes: 2

Views: 872

Answers (1)

Rohit
Rohit

Reputation: 535

You can use a fork and pass a message from the child process to the main process instead of console logging.

Something like this:

On the app.js

import { resolve } from "path";
import cp from "child_process";

const child = cp.fork(resolve(__dirname, "worker.js"));
child.send({ env });

child.on("message", (m) => console.log("Message from child", m));
child.once("exit", () => console.log("Process exited PID ", child.pid))

And on the worker.js

// Do the required work
process.on("message", ({env}) => {
    process.send("the thing you where logging");
});

Upvotes: 1

Related Questions