开发者

Why does this simple usage of Python's subprocess hang?

开发者 https://www.devze.com 2023-03-29 14:02 出处:网络
import subprocess proc1 = subprocess.Popen([\'cat\'], stdout=subprocess.PIPE, stdin=subprocess.PIPE) proc2 = subprocess.Popen([\'cat\'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
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

0

精彩评论

暂无评论...
验证码 换一张
取 消