python - 重定向当前正在运行的 python 进程的标准输出
问题描述
我有运行 C 代码的 Python 代码,这些 C 代码向标准输出和标准错误发送垃圾邮件。
我想抑制标准输出和标准错误,而不能修改 C 代码。作为伪代码,大致:
def external_api_I_cant_control():
C code that outputs to stdout & stderr # this is not a subprocess
# call which I can redirect
# its stdout & stderr. Rather,
# it's a wrapper for it so there
# is nothing I can do to control its
# FD
def do_stuff():
external_api_I_cant_control()
我的代码正在运行 using python bla.py
,因此我可以重定向stderr
using python bla.py 2>/dev/null
,但这并不能解决我的问题,因为有些垃圾邮件是到的stdout
,我无法重定向stdout
- 因为我需要其中的一些。
是否可以在我的代码中执行相当于 shell 标准输出重定向的操作?
到目前为止我尝试过:
contextlib.redirect_stdout
- 替换
sys.stdout
和sys.__stdout__
_open(os.devnull, 'w')
我想要的结果相当于将我bla.py
分成 3 份并像这样运行它:
python bla0.output.is.legit.py
python bla1.output.should.be.disregarded.py &> /dev/null
python bla2.output.is.legit.again.py
实现这一目标的方法是什么?
解决方案
这是一份工作os.dup2()
。您想要的代码包含三个阶段:
- 备份原始 stdout 和 stderr
- 用指向的文件句柄替换 stdout 和 stderr
/dev/null
- 恢复备份(在运行您想要的代码之后)。
# create a backup of stdout and stderr
orig_stdout_fd = os.dup(1)
orig_stderr_fd = os.dup(2)
# make the original stdout and stderr point to /dev/null
devnull = open('/dev/null', 'r+')
os.dup2(devnull.fileno(), 1)
os.dup2(devnull.fileno(), 2)
# run your C code here
os.write(1, b"This would normally go to stdout\n")
# put the originals back
os.dup2(orig_stdout_fd, 1)
os.dup2(orig_stderr_fd, 2)
os.close(orig_stdout_fd)
os.close(orig_stderr_fd)