Continuing from my previous question I see that to get the error code of a process I spawned via Popen in python I have to call either wait() or communicate() (which can be used to access the Popen stdout and stderr attributes):
app7z = '/path/to/7z.exe' command = [app7z, 'a', dstFile.temp, "-y", "-r", os.path.join(src.Dir, '*')] process = Popen(command, stdout=PIPE, startupinfo=startupinfo) out = process.stdout regCompressMatch = re.compile('Compressing\s+(.+)').match regErrMatch = re.compile('Error: (.*)').match errorLine =  for line in out: if len(errorLine) or regErrMatch(line): errorLine.append(line) if regCompressMatch(line): # update a progress bar result = process.wait() # HERE if result: # in the hopes that 7z returns 0 for correct execution dstFile.temp.remove() raise StateError(_("%s: Compression failed:\n%s") % (dstFile.s, "\n".join(errorLine)))
However the docs warn that
wait() may deadlock (when stdout=PIPE, which is the case here) while
communicate() might overflow. So:
- what is the proper thing to use here ? Note that I do use the output
how exactly should I use communicate ? Would it be:
process = Popen(command, stdout=PIPE, startupinfo=startupinfo) out = process.communicate() # same as before... result = process.returncode if result: # ...
not sure about blocking and the memory errors
- Any better/more pythonic way of handling the problem ? I do not think that
subprocess.check_call/check_outputapply in my case - or do they ?
DISCLAIMER: I did not write the code, I am the current maintainer, hence question 3.
- Python popen command. Wait until the command is finished
- Check a command's return code when subprocess raises a CalledProcessError exception
- wait process untill all subprocess finish?
I am on windows if this makes a difference - python 2.7.8
There should be one-- and preferably only one --obvious way to do it