I have a program that needs to be called as a subprocess with python. The program has been written in java. yeah, i know...
anyway, I need to capture all of the output from said program.
Unfortunately, when I call subprocess.popen2 or subprocess.Popen with communicate[0], I'm losing around 10% of the output data when I'm using a subprocess.PIPE assigned to stdout AND when i'm using a file descriptor (the return from an open) assigned to stdout.
The documentation in subprocess is pretty explicit that using subprocess.PIPE is volatile if you're trying to capture all of the output from a child process.
I'm currently using pexpect to dump the ouput into a tmp file but that's taking forever for obvious reasons.
I'd like to keep all the data in memory to avoid disk writes.
any recommendations are welcome! thanks!
import subprocess
cmd = 'java -Xmx2048m -cp "/home/usr/javalibs/class:/home/usr/javalibs/libs/dependency.jar" --data data --input input"
# doesn't get all the data
#
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
output = p.communicate()[0]
OR
# doesn't get all the data
#
fd = open("outputfile",'w')
p = subprocess.Popen(cmd, stdout=fd, shell=True)
p.communicate()
fd.close() # tried to use fd.flush() too.
# also tried
# p.wait() instead of p.communicate(), but wait doesn't really wait for the java program to finish running - it doesn't block
OR
# also fails to get all the data
#
import popen2
(rstdout, rstdin) = popen2.popen2(cmd)
Expected output is a series of ascii lines (a couple thousand). the lines contain a number and an end of line character
0\n
1\n
4\n
0\n
...