質問

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!

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top