I have a node.js script which starts a python subprocess and reads its stdout. This works as long as the python process does not try to read from stdin. Then the parent process does not get anything from the child.
I have the node.js script and two python test cases here: (both examples work if you comment the lines that try to read from stdin)
First child:
import sys
print('before')
for line in sys.stdin:
print(line)
print('after')
Second child:
import sys
print('before')
while True:
line = sys.stdin.readline()
if line != '':
print(line)
else:
break
print('after')
Parent:
const spawn = require('child_process').spawn;
let client = spawn('python', ['test1.py'], {cwd: '/tmp'});
client.stdout.on('data', (data) => {
console.log(data.toString());
});
client.stderr.on('data', (data) => {
console.log(data.toString());
});
client.on('close', () => {
console.log('close');
});
client.on('exit', () => {
console.log('exit');
});
client.on('disconnect', () => {
console.log('disconnect');
})
print('before', flush=True)to send immediately. Then it waits for data and ... well... you need to send data.flush=Truetrick did solve the problem. If you post this as an answer I will accept it :)node.jsside with something likeconst spawn = require('pty.js').spawn;This question discusses splitting stdout/err streams stackoverflow.com/questions/15339379/…. I'm happy to supply an answer... but I'm not sure its the best answer.