I've got a node.js application that needs to pass input to a python program and get the input back. This process needs to occur frequently and with as little latency as possible. Thus I've chosen to use the child_process package. The javascript code is below
child_process = require('child_process');
class RFModel {
constructor () {
this.answer = null;
this.python = child_process.spawn('python3', ['random_forest_model.py']);
this.python.stdout.on('data', (data)=>{
this.receive(data);
});
}
send (data) {
this.answer = null;
this.python.stdin.write(data);
}
receive (data) {
data = data.toString('utf8')
console.log(data)
this.answer = data;
}
}
const model = new RFModel()
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
(async ()=>{
await timeout(7000)
model.send('asdf')
})();
And this is the python code:
import sys
sys.stdout.write('test')
sys.stdout.flush()
for line in sys.stdin:
open("test.txt", "a") #janky test if input recieved
sys.stdout.write(line)
sys.stdout.flush()
The nodejs process receives the first "test" output from the python process, but it does not seem like python is receiving any input from nodejs, since there is no echo or text file being created. What am I doing wrong?