I have Python code that runs an external application that works fine when the application has a small amount of output, but freezes when there is a lot. My code looks like this:
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) errcode = p.wait() retval = p.stdout.read() errmess = p.stderr.read() if errcode: log.error('cmd failed <%s>: %s' % (errcode,errmess))
The docs have comments that seem to indicate a potential problem. Waiting to eat:
Warning. This will be inhibited if the child process generates enough output to the stdout or stderr channel, so that it blocks waiting for the OS buffer to receive more data. Use communicate() to avoid this.
although I communicate, I see:
Note. Reading data is buffered in memory, so do not use this method if the data size is large or unlimited.
Therefore, it is not clear to me that I should use any of them if I have a large amount of data. They do not indicate which method I should use in this case.
I need to return the value from exec and do parsing and use both stdout and stderr .
So, what is the equivalent method in Python for executing an external application that will have great output?
python subprocess
Tim Jul 24. '09 at 23:08 2009-07-24 23:08
source share