title: Python Popen - wait vs communicate vs CalledProcessError
body: <p>Continuing <a href="https://stackoverflow.com/a/30967043/281545">from my previous question</a> 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):</p>
  
  <pre><code>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)))
  </code></pre>
  
  <p>However <a href="https://docs.python.org/2/library/subprocess.html#popen-objects" rel="noreferrer">the docs</a> warn that <code>wait()</code> may deadlock (when stdout=PIPE, which is the case here) while <code>communicate()</code> might overflow. So:</p>
  
  <ol>
  <li>what is the proper thing to use here ? Note that I do use the output</li>
  <li><p>how exactly should  I use communicate ? Would it be:</p>
  
  <pre><code>process = Popen(command, stdout=PIPE, startupinfo=startupinfo)
  out = process.communicate()[0]
  # same as before...
  result = process.returncode
  if result: # ...
  </code></pre>
  
  <p>not sure about blocking and the memory errors</p></li>
  <li>Any better/more pythonic way of handling the problem ? I do not think that <a href="https://stackoverflow.com/a/25079374/281545"><code>subprocess.CalledProcessError</code> or the <code>subprocess.check_call/check_output</code></a> apply in my case - or do they ?</li>
  </ol>
  
  <p>DISCLAIMER: I did not write the code, I am the current maintainer, hence question 3.</p>
  
  <p>Related:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2837214/python-popen-command-wait-until-the-command-is-finished">Python popen command. Wait until the command is finished</a></li>
  <li><a href="https://stackoverflow.com/questions/15316398/check-a-commands-return-code-when-subprocess-raises-a-calledprocesserror-except">Check a command&#39;s return code when subprocess raises a CalledProcessError exception</a></li>
  <li><a href="https://stackoverflow.com/questions/15107714/wait-process-untill-all-subprocess-finish">wait process until all subprocess finish?</a></li>
  </ul>
  
  <p>I am on windows if this makes a difference - python 2.7.8</p>
  
  <p><em>There should be one-- and preferably only one --obvious way to do it</em></p>