The below function, startScript
, contains blocking code that takes some time to run. In my setInterval
loop, I want to access the weight
value that it returns. However, when I run this Node program I receive an error saying that new_weight
is undefined.
I tried running startScript.on('close', ...)
within the loop, but this throws an error saying that there is no method on
for startScript
What am I doing wrong here?
var spawn = require('child_process').spawn
var path = require('path');
var split = require('split');
var pythonData = [];
var weight = null;
function startScript(){
var pyScript = spawn('python', [ path.join(__dirname, 'script.py') ]);
pyScript.stdout.on('data', function(lineChunk){
pythonData = lineChunk.toString().replace(/[\S|\n|\[|\]]/,"").split(',');
});
pyScript.on('close', function(code){
var sum = 0;
for(var i=0; i < pythonData.length; i++){
sum += parseFloat(pythonData[i]);
}
var weight = sum / pythonData.length;
console.log("weight: " + weight);
return weight;
});
}
setInterval(function(){
if (some event that occurs infrequently){
startScript();
var new_weight = weight + 100
console.log(new_weight);
}
}, 1000);
weight
. and you never accepted a return value from start script.null
. ifstartScript
is givingweight
a value, then you would be able to access it the way you are. jsfiddle.net/86z3Z