0

I'm now working on a server written in nodejs, but a problem occurred. I wrote a function(written in nodejs, obviousely) which calls a module written in python. To get the return value from the python module, I used spawn.stdout.on method in the caller function(nodejs)

It worked really good.... unless I call the function(nodejs) in the other function. I don't know why it happens... Below is my code.

  • The function that calls the nodejs function

 .then(async function (result) {
        let temp=await grade_5(result);
        return (temp);
    })

  • The function that is called by the upper, and calls the function written in python. This function is the problematic one.

I've checked that the data is successfully retrieved from the python module by executing console.log(result) right under the const result~. The only thing that doesn't work is the method: result.stdout.on(). The code flow doesn't get in the callback function of it.


grade_5 = async function (num_Rate){
    let numRate = num_Rate
    let maxIdx=numRate.length
    const spawn = require('child_process').spawn;
    let rate=[];
    let id=[];
    let rate_result=[];
    let result_arr=[];

    for(var i=0;i<maxIdx;i++){
        id.push(numRate[i][0]);
        rate.push(numRate[i][1]);
    }

    const result= spawn('python', ['routes/analysis/grade_Calculate.py', rate]);

    // where the code flow can't get in
    await result.stdout.on('data', function(data){
        console.log(data.toString());
        let temp=data.toString().replace("[", "");
        temp=temp.replace("]", "")
        temp=temp.split(", ");

        for(var i=0;i<maxIdx;i++){
            rate_result.push(Number(temp[i]));
        }
        // ============data preprocessing end=============
        console.log(rate_result);

        //compare data with the standard normal distribution chart, and input it into the new array which will be result_arr=[[id, rate]]
        let result_arr=[];
        for(var i=0;i<maxIdx;i++){
           ...
        }
        console.log(result_arr);
    })
    
result.stderr.on('data', function(data){
        console.log(data.toString());
        return(-1)
    })
    console.log("this is the end");
    return(result_arr);
}

  • The function which written in python

.
.
.

if __name__=='__main__':
    rate_z_standardized=z_score_normalization(sys)
    rate_mm_normalized=min_max_normalization(rate_z_standardized)
    rate_log=natural_log(rate_mm_normalized)
    print(rate_log)

If anyone knows something, please help me...

toubva
  • 3
  • 2
  • Returning a value from your spawn `.on()` callbacks will NOT return that value from your main `grade_5()` function. In fact, you have no return value at all from your `grade_5()` function, See the question yours was marked a duplication of for further explanation and options. – jfriend00 Jun 12 '22 at 15:50
  • okay, then I'll edit the code that will return the value that I want outside of a `.on()` callback. However, the problem is, that 'the code flow doesn't even get inside of the `.on()` callback'... `console.log()` inside of the callback function doesn't work. – toubva Jun 12 '22 at 15:53
  • Please go read the [question](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) yours is marked a duplicate of. You CANNOT directly return a value that is obtained asynchronously as your function will return BEFORE the asynchronous callbacks are called. Instead, you have to use a callback or a promise or an event to communicate back an asynchronously-retrieved value (modern implementations would use a promise). This is described in the duplicate. I marked yours as a duplicate rather than repeating everything that is already covered there. – jfriend00 Jun 12 '22 at 17:00
  • Also, the `await` here `await result.stdout.on(...)` is pointless. `await` only does something useful when you're awaiting a promise which is not what `result.stdout.on(...)` returns. – jfriend00 Jun 12 '22 at 17:03
  • You also have a problem with your `data` event handler because `data` is not guaranteed to all come at once. It might come in pieces/chunks. – jfriend00 Jun 12 '22 at 17:04
  • If your code NEVER gets inside the `.on()` callbacks, then you have a more fundamental problem, probably with your `spawn()` command. You should write error handling code for the spawn to log any errors you might be seeing. – jfriend00 Jun 12 '22 at 17:06

0 Answers0