Home > front end >  Node js. How to pass a variable to child process?
Node js. How to pass a variable to child process?

Time:11-15

I'm trying to understand how child_process works and I don't see any options to pass a variable to the child. My situation:

Main.js

[const arr = [10,20,30,40,50]


for (let g of arr ){
      
    function(g) // I want to launch every loop in a separate process because there are a lot of async functions inside and it takes a lot of time to process.
    
  }

First I tried child_process.fork :

//Main.js

const { fork } = require('child_process');

const forked = fork('test/child.js');


for (let g of arr){
      
    forked.send(g);
    
  }

//Child.js

process.on('message', (msg) => {
    console.log('Message from parent:', function(msg));
  });

Child.js received variable g, but the process of the function's execution became much longer than a simple loop.

Second I tried child_process.execFile

//I made a simple loop from 0 to 5 to see the speed without sending variables

for (let i = 0;i< arr.length;i  ){ 
      
    
const { execFile } = require('child_process');
  

const child = execFile('node', ['test/child.js'], 
        (error, stdout, stderr) => {
  if (error) {
    throw error;
  }
  console.log(stdout);
});
}

Here I got a perfect result. 5 fast and paralleled executions. But as I see there is no way to send this integer from the array to the child.js and other child methods allow only to send arguments for the command line, but not variable? Maybe there are some ways to do it? Sorry for my confusing text and thanks.

CodePudding user response:

First I tried child_process.fork

The problem with your attempt was that you did fork only a single child process, not 5 of them. Then you sent 5 messages to the same process. You'll want

// Main.js
const { fork } = require('child_process');
for (let g of arr) {
    const forked = fork('test/child.js'); // inside the loop!
    forked.send(g);
}
// Child.js
process.on('message', (msg) => {
    console.log('Message from parent:', function(msg));
});

CodePudding user response:

I solved my problem. May be it's a primitive solution but it works.

Main.js

const { execFile } = require('child_process');
require('dotenv').config()
async function Main(){
  
  arr = [0,1,2,3,4,5,6,7,8,9,10]

  for (i = 0;i<arr.length;i  ){

  process.env.msg = arr[i]
  
  await new Promise(resolve => setTimeout(resolve, 50));
    Child()
    
  }

}

 function Child(){
  const child = execFile('node', ['test/child.js'], 
    (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
    });
 }
Main()

Child.js

const { execFile } = require('child_process');

console.log(process.env.msg)
  • Related