나는 python 서브 프로세스를 시작하고 stdout을 읽는 node.js 스크립트를 가지고 있습니다. 이것은 파이썬 프로세스가 stdin에서 읽으 려하지 않는 한 작동합니다. 그렇다면 부모 프로세스는 자식으로부터 아무 것도 얻지 못합니다.Node.js는 stdin에서 읽을 때 파이썬 subprocess stdout을 읽을 수 없습니다
첫째 아이 (당신은 표준 입력에서 읽으려고 줄 언급하는 경우 두 예제가 작동) :
import sys
print('before')
for line in sys.stdin:
print(line)
print('after')
둘째 아이를
본인은 Node.js를 스크립트와 여기에 두 파이썬 테스트 케이스를 :
import sys
print('before')
while True:
line = sys.stdin.readline()
if line != '':
print(line)
else:
break
print('after')
부모 :
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');
})
node.js를 모르지만 파이썬 관점에서 볼 때 한 줄짜리지만, 파이프가 아닌 tty이기 때문에 더 많은 데이터를 기다리는 동안 버퍼링됩니다. 'print (before ', flush = True)'를 사용하면 즉시 보낼 수 있습니다. 그런 다음 데이터를 기다리고 ... 음 ... 데이터를 보내야합니다. – tdelaney
'flush = True' 트릭이 문제를 해결했습니다. 이 질문을 답으로 게시하면 받아 들일 것입니다 :) – Martin
'node.js' 측에서'const spawn = require ('pty.js'). spawn;과 같이 수정하는 것이 더 나을 것 같습니다. stdout/err 스트림 분할에 대해 설명합니다. http://stackoverflow.com/questions/15339379/node-js-spawning-a-child-process-interactively-with-separate-stdout-and-stderr-s. 나는 대답을 해줘서 기쁩니다 ...하지만 나는 그 최선의 대답을 확신하지 못합니다. – tdelaney