Eu acabei hackeando essa coisa juntos:
import subprocess
import time
import sys
log = open(sys.argv[3], 'w')
input = open(sys.argv[2], 'r')
p = subprocess.Popen([sys.argv[1]], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def readAllSoFar(proc, retVal=''):
while (subprocess.select.select([proc.stdout],[],[],0)[0]!=[]):
retVal+=proc.stdout.read(1)
return retVal
while True:
print('starting loop')
line = input.readline()
print('read line ' + line)
if not line:
print('breaking')
break
print('before sleep1')
time.sleep(0.1)
print('before readAllSoFar')
output = readAllSoFar(p)
print('got output' + output)
log.write(output)
time.sleep(0.1)
log.write(line)
p.stdin.write(line)
Execute com python runner.py ./program_to_run input.txt output.txt
Assume-se que qualquer resultado de um comando será feito em um décimo de segundo.