0

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.

1 Answer 1

1

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;
}

Sign up to request clarification or add additional context in comments.

5 Comments

Still not working checkout this screenshot printing blank Image .PS - generateCanadaScore function output const printing data correctly
@Vaibhavdadhich make sure to accept his answer if it helped you, so others know that it's a viable solution for similar issues.
You're not showing any code that would hook up the function to a request handler, you're just calling it once after the app starts. I doubt that's what you'd want for an API.
Thanks a lot @AKX It worked when hooked with request handler , now out of curiosity my question is why it was not working when using with app.listen ?
Because the app.listen callback is just for knowing when the app is listening, nothing more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.