I’m using subprocess to run a script , get the output of the script on a pipe and process on the output .
I experience a weird problem where in sometimes it reads till the end of the script and someother time it does not go till the end.
I suspect this could be a problem with the buffer size .. tried few alternatives but haven’t been succesful yet..
def main():
x = subprocess.Popen('./autotest', bufsize = 1, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, cwd = '/home/vijay/run/bin', shell = True)
with open("out.txt",'wb') as f:
for line in x.stdout:
if 'Press \'q\' to quit scheduler' in line:
print line.strip()
f.write(line.strip())
x.stdin.write('q')
f.write('\n')
x.stdin.close()
x.stdout.flush()
try:
x.stdout.read()
except:
print 'Exception Occured !!!'
os._exit(1)
else:
print line.strip()
f.write(line.strip())
f.write('\n')
x.stdout.flush()
if __name__ == '__main__':
main()
You should keep trying to read from stdout until the process terminates not until stdout ends, use poll() to check if the process terminated and if not, try to read again.