0

I was working on a code using python and node. I called python code using child-process in node js to use its outputs.

  var images_score = 0
  const python = spawn('python',[__dirname + "/car_detect/main.py", img_name]);
  python.stdout.on('data', (data) => {
    images_score = Number(data.toString());
    console.log(images_score);
  })
}

if (images_score == 1){
//some code here
}else{
console.log('err');
}

 

but before I receive the result (data.toString() == '1'), node continues the program without giving the output of my python code and shows me 'err' in the console.

  • Put `some code here` to `data` callback like `console.log(images_score);` – hoangdv Oct 28 '22 at 09:14
  • 1
    The `if (images_score == 1)` is executed immediately after you start the `python` subprocess, not when it has finished. You need to wait for that, and run the code when the child process ends. – Bergi Nov 01 '22 at 15:23

0 Answers0