Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
Node Child Process (Spawn) is not returning data correctly when using with function

I am trying to generate a new score based on the output of my python script. The python script returning the data correctly and JS program printing correctly but the problem is when i return the value and print it, it shows undefined

Function Code -

async function generateCanadaScore(creditscore = 0) {
  console.log(creditscore, "creditscore"); //prints 300
  let MexicoData = 0;
  const python = spawn("python", [
    "cp_production.py",
    "sample_dill.pkl",
    "mexico",
    Number(creditscore),
  ]);

  await python.stdout.on("data", function (data) {
    console.log("Pipe data from python script ...");
    console.log(data.toString()); //prints number 
    MexicoData = data.toString();
    console.log(MexicoData) // prints number
//working fine till here printing MexicoData Correctly (Output from py file) , problem in return
    return MexicoData ;
  });
  python.stderr.on("data", (data) => {
    console.log(data); // this function doesn't run
  });
// return MexicoData ; already tried by adding return statement here still same error
}

Calling Function Code -

app.listen(3005, async () => {
  console.log("server is started");
  //function calling 
  // Only for testing purpose in listen function 
  let data = await generateCanadaScore(300);
  console.log(data, "data"); // undefined
});

I will not be able to share python code it is confidential.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't await on the event handler. (It returns undefined, so you're basically doing await Promise.resolve(undefined), which waits for nothing).

You might want to wrap your child process management using new Promise() (which you'll need since child_process is a callback-async API, and you need promise-async APIs):

const {spawn} = require("child_process");

function getChildProcessOutput(program, args = []) {
  return new Promise((resolve, reject) => {
    let buf = "";
    const child = spawn(program, args);

    child.stdout.on("data", (data) => {
      buf += data;
    });

    child.on("close", (code) => {
      if (code !== 0) {
        return reject(`${program} died with ${code}`);
      }
      resolve(buf);
    });
  });
}

async function generateCanadaScore(creditscore = 0) {
  const output = await getChildProcessOutput("python", [
    "cp_production.py",
    "sample_dill.pkl",
    "mexico",
    Number(creditscore),
  ]);
  return output;
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!