【发布时间】:2023-05-14 05:17:02
【问题描述】:
这是我在 python 子进程模块文档中可以阅读的内容:
Replacing shell pipeline
output=`dmesg | grep hda`
==>
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]
The p1.stdout.close() call after starting the p2 is important in order for p1
to receive a SIGPIPE if p2 exits before p1.
我真的不明白为什么我们必须在创建 p2 之后关闭 p1.stdout。 p1.stdout.close() 何时执行? 当 p2 永远不会结束时会发生什么? 当 nor p1 或 p2 结束时会发生什么?
【问题讨论】:
标签: python shell subprocess pipeline