标准输出(或标准输出)设置为子进程的确切含义是什么.管道在波彭?



我已经阅读了有关python中子进程的文档,但仍然无法完全理解这一点。

当使用Popen时,我们将参数stdout(或stdin(设置为subprocesses.PIPE,这实际上意味着什么?

文件说

stdin、stdout 和 stderr 指定已执行程序的标准 输入、标准输出和标准错误文件句柄, 分别。。。PIPE 指示到子项的新管道应为 创建。

这是什么意思?

例如,如果我有两个子进程,都带有 stdout 到 PIPE,那么 ouptuts 是否混合在一起?(我不这么认为(

更重要的是,如果我有一个将标准输出设置为 PIPE 的子进程,然后又将 stdin 设置为 PIPE 的另一个子进程,该管道是否相同,一个的输出转到另一个?

有人可以解释一下对我来说似乎很糟糕的文档部分吗?


附加说明: 例如

import os
import signal
import subprocess
import time
# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before  exec() to run the shell.
pro = subprocess.Popen("sar -u 1 > mylog.log", stdout=subprocess.PIPE, 
shell=True, preexec_fn=os.setsid) 
// Here another subprocess
subprocess.Popen(some_command, stdin=subprocess.PIPE)
time.sleep(10)
os.killpg(os.getpgid(pro.pid), signal.SIGTERM) 

sar 的输出是否作为"某个命令"的输入?

请参阅文档。

如您所见,PIPE是一个特殊值,它"表示应创建通往子项的新管道"。这意味着,stdout=subprocess.PIPEstderr=subprocess.PIPE会产生两个不同的管道。

对于您的示例,答案是否定的。这是两个不同的管道。

实际上您可以打印出subprocess.PIPE

print(subprocess.PIPE)
# -1
print(type(subprocess.PIPE))
# int
# So it is just an integer to represent a special case.

最新更新