[英]What is the difference if I don't use stdout=subprocess.PIPE in subprocess.Popen()?
[英]What exactly means that the stdout (or stdin) is set to subprocess.PIPE in Popen?
我已經閱讀了 python 中關於子流程的文檔,但仍然不太明白這一點。
使用 Popen 時,我們將參數stdout
(或stdin
)設置為subprocesses.PIPE
,這實際上意味着什么?
文件說
stdin、stdout 和 stderr 分別指定執行程序的標准輸入、標准 output 和標准錯誤文件句柄... PIPE 表示應該為子進程創建一個新的 pipe。
這是什么意思?
例如,如果我有兩個子進程的標准輸出都是 PIPE,那么輸出是混合的嗎? (我不這么認為)
更重要的是,如果我有一個 stdout 設置為 PIPE 的子進程,后來又有一個 stdin 設置為 PIPE 的子進程,那么 pipe 是否相同,一個的 output 轉到另一個?
有人可以向我解釋文檔中對我來說似乎很苛刻的部分嗎?
補充說明:例如
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 的 output 是否作為“某些命令”的輸入?
請參閱文件。
正如您所見, PIPE
是一個特殊值,它“表示應該為孩子創建一個新的 pipe”。 這意味着, stdout=subprocess.PIPE
和stderr=subprocess.PIPE
導致兩個不同的管道。
對於您的示例,答案是否定的。 這是兩個不同的管道。
實際上你可以打印出subprocess.PIPE
:
print(subprocess.PIPE)
# -1
print(type(subprocess.PIPE))
# int
# So it is just an integer to represent a special case.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.