gpt4 book ai didi

如果创建,Python 子进程 stderr/stdout 字段为 None

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:44 24 4
gpt4 key购买 nike

当我用一个文件流作为stderr(或stdout)创建一个python子进程时,对应的字段是None:

s = subprocess.Popen(['ls','qwer'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=open("zzzzz",'wb'))
s.stderr is None
==> True

(我希望 s.stderr 是文件流)。

  • 这是记录在案的/故意的吗?
  • 理由是什么?

最佳答案

是的,这是故意的。当您向 Popen constructor 提供 stdout/stdin/stderr kwargs 时,您要告诉它在子进程中将这些管道指向何处:

stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively.

但是 Popen 对象上的 stdout/stdin/stderr 属性 are documented as only having a value when you use PIPE :

Popen.stdin

If the stdin argument was PIPE, this attribute is a file object that provides input to the child process. Otherwise, it is None.

Popen.stdout

If the stdout argument was PIPE, this attribute is a file object that provides output from the child process. Otherwise, it is None.

Popen.stderr

If the stderr argument was PIPE, this attribute is a file object that provides error output from the child process. Otherwise, it is None.

至于当你传递一个不是PIPE 的句柄时,为什么Popen.std*None,我不确定。也许是因为它被认为是多余的,因为您必须首先将一个打开的句柄传递给构造函数?

关于如果创建,Python 子进程 stderr/stdout 字段为 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25370347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com