当我浏览帖子时,我在 here 上遇到了下面这个例子, 它说 proc1.stdout.close()
需要被调用以适当退出 proc1
, 生成 SIGPIPE
.
import subprocess
proc1 = subprocess.Popen(['ps', 'cax'], stdout=subprocess.PIPE)
proc2 = subprocess.Popen(['grep', 'python'], stdin=proc1.stdout,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
out, err = proc2.communicate()
print('out: {0}'.format(out))
print('err: {0}'.format(err))
不过,我不是很清楚。请修正我的理解。
-
SIGPIPE
当PIPE
时发生尝试写入已关闭的PIPE
. - 作家
PIPE
是proc1
的stdout
和阅读器PIPE
是proc2
的stdin
. -
proc1
将在proc2
时退出退出和proc1
尝试将数据写入proc2
的stdin PIPE
. 因为-
proc2
的stdin PIPE
proc2
时关闭退出 -
SIGPIPE
发生在proc1
因为proc1
尝试写入已关闭的proc2
的stdin PIPE
.
-
据我了解,SIGPIPE
会发生并且proc1
将退出,无论关闭 proc1
的 stdout
.
我错过了什么?
编辑
看完post来自@unutbu 的评论......
我认为复制的文件描述符(proc1.stdout
)是写入者 PIPE,而不是读者 PIPE。因此,有两个写入器 PIPE 和一个读取器 PIPE 相互连接。
因此,SIGPIPE
将在 proc2
时生成退出因为proc2
只有一个进程有读取器 PIPE(将在 proc2
退出时关闭)。
然而,以上post似乎是说通过复制 proc1.stdout
有两个读者 PIPE所以SIGPIPE
即使在 proc2
之后也不会生成退出,因为还有另一个阅读器 PIPE 打开。以下是post的一部分.
So by closing p1.stdout immediately, you ensure that the only remaining filehandle reading from dmesg stdout is the grep process, and if that process were to exit, dmesg receives a SIGPIPE.
我不是说 post是错误的,但我只想修正我的理解。提前谢谢你。
最佳答案
proc1 = subprocess.Popen(['ps', 'cax'], stdout=subprocess.PIPE)
在父进程和 proc1
之间创建一个管道:
| | | |
| parent |-<-----<-| proc1 |
| | ^ | |
|
p1.stdout
p1.stdout
是父级为从 proc1
获取(stdout)输出而读取的内容。
proc2 = subprocess.Popen(['grep', 'python'], stdin=proc1.stdout,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
将管道的副本从 proc1 连接到 proc2:
| | | | | |
| parent |-<-----<-| proc1 |->----->-| proc2 |
| | | | | |
通过调用 p1.stdout.close()
,我们关闭管道的父进程端:
| | | | | |
| parent | <-| proc1 |->----->-| proc2 |
| | | | | |
现在当 proc2
终止时,它的管道一侧也关闭了:
| | | | | |
| parent | <-| proc1 |-> | proc2 |
| | | | | |
下一次 proc1
尝试写入管道时,会生成一个 SIGPIPE 信号,
这允许 proc1
终止,因为它知道没有人在其管道的另一端监听。
关于Python subprocess.Popen PIPE 和 SIGPIPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48001819/