【发布时间】:2013-09-24 04:37:10
【问题描述】:
代码 sn-p 来自:http://docs.python.org/3/library/subprocess.html#replacing-shell-pipeline
output=`dmesg | grep hda`
# becomes
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
问题:我不太明白为什么需要这条线:p1.stdout.close()?
如果这样做 p1 stdout 甚至在它完全完成输出数据并且 p2 还活着之前就关闭了怎么办?我们这么快就关闭p1.stdout 是不是有风险?这是如何工作的?
【问题讨论】:
标签: python