16

In the document of wait (http://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait), it says:

Warning

This will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that.

From this, I think communicate could replace all usage of wait() if retcode is not need. And even when the stdout or stdin are not PIPE, I can also replace wait() by communicate().

Is that right? Thanks!

Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
  • 2
    `communicate` is a convenience method that hides the platform-dependent details of reading/writing to the pipes using poll, select, or threads (Windows). It calls `wait` at the end. Use if it suits your needs, but a more complex pipeline might require handling the pipes manually, or use an intermediate `wait`. – Eryk Sun Nov 23 '12 at 18:05
  • @eryksun Will `wait()`'s performance be better than `communicate()`? – Hanfei Sun Nov 24 '12 at 03:35
  • 3
    I can imagine a situation where you decide to close the pipes to the subprocess, before it finishes running. When (later) you want to make sure it has finished, a wait() will work, but not a communicate(), because it would get confused by the already-closed pipes. – Armin Rigo Nov 24 '12 at 10:57

1 Answers1

7

I suspect (the docs don't explicitly state it as of 2.6) in the case where you don't use PIPEs communicate() is reduced to wait(). So if you don't use PIPEs it should be OK to replace wait().

In the case where you do use PIPEs you can overflow memory buffer (see communicate() note) just as you can fill up OS pipe buffer, so either one is not going to work if you're dealing with a lot of output.

On a practical note I had communicate (at least in 2.4) give me one character per line from programs whose output is line-based, that wasn't useful to put it mildly.

Also, what do you mean by "retcode is not needed"? -- I believe it sets Popen.returncode just as wait() does.

dima
  • 86
  • 1