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...