Skip to main content
 首页 » 编程设计

Python 函数捕获子进程 stdout 和 stderr 到日志文件

2024年10月01日9jpfss

这里用少量代码完成了很多事情。我会尽量保持简洁。

我有一个运行外部程序并将 stdout 和 stderr 发送到日志文件的 python 函数。

我正在使用 doctest 来测试该功能。我需要测试输出捕获功能。下面的代码显示了我尝试编写函数和测试。测试失败,没有任何内容写入日志文件。我不确定问题是出在测试中还是出在被测代码中,或者两者都有。有什么建议吗?

from __future__ import print_function 
 
import subprocess 
 
def run(command_line, log_file): 
    """ 
    # Verify stdout and stderr are both written to log file in chronological order 
    >>> run("echo text to stdout; echo text to stderr 1>&2", "log") 
    >>> f = open("log"); out = f.read(); f.close() 
    >>> print(out.strip()) 
    text to stdout 
    text to stderr 
    """ 
    command_line = "set -o pipefail; " + command_line + " 2>&1 | tee " + log_file 
 
    # Run command. Wait for command to complete. If the return code was zero then return, otherwise raise CalledProcessError 
    subprocess.check_call(command_line, shell=True, executable="bash") 

测试结果:

$ python -m doctest testclass.py 
text to stdout 
text to stderr 
********************************************************************** 
File "testclass.py", line 10, in testclass.run 
Failed example: 
    print(out.strip()) 
Expected: 
    text to stdout 
    text to stderr 
Got: 
    <BLANKLINE> 
********************************************************************** 
1 items had failures: 
   1 of   3 in testclass.run 
***Test Failed*** 1 failures. 

请您参考如下方法:

自从使用 shell=True 执行 subprocess.check_call 后,使用 2 个 stdout/stderr 重定向和一个 tee 并不是最好的方法执行命令并捕获输出(实际上它最接近最差方式),我对它失败并不感到惊讶。

我的解决方案是为初学者删除 set -o pipefail(您不需要在此处检查返回代码)并将两个命令都放在括号中,否则重定向/tee 仅适用于最后一个一个(老实说,我仍然很困惑为什么你没有得到任何输出):

command_line = "(" + command_line + ") 2>&1 | tee " + log_file 

如果您必须恢复 pipefail 事情,请在括号内执行:

command_line = "(set -o pipefail; " + command_line + ") 2>&1 | tee " + log_file