Why does this simple usage of Python's subprocess hang?
import subprocess
proc1 = subprocess.Popen(['cat'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
proc2 = subprocess.Popen(['cat'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
proc1.stdin.close()
proc1.wait开发者_如何学Go()
When running this on Debian with Python 2.6.6, the program never exits. The strange thing is when I change the last two lines to use proc2
, it does exit.
What's going on here?
Try typing "cat" with no arguments in a terminal. "cat" requires something to concatenate to the stdout.
>>> from subprocess import Popen, PIPE
>>> args = ["cat", "foobar"]
>>> reply = Popen(args, stdout=PIPE).communicate()[0]
>>> print (reply)
line 0
line 1
line 2
>>>
Docs have a pretty good set of use examples:
http://docs.python.org/library/subprocess.html#replacing-shell-pipeline
精彩评论