子进程命令的实时输出状态 Python
Live output status from subprocess command Python
我正在编写脚本以使用 subprocess.check_output 获取 netstat 状态。
cmd = 'netstat -nlpt'
result = subprocess.check_output(cmd, shell=True, timeout=1800)
print(result.decode('utf-8'))
以上是运行完美。有什么办法可以得到直播输出。我听说 poll() 函数可以完成这项工作。在 live output from subprocess command 他们正在使用 popen 但我正在使用 check_output 请有人帮助我解决这个问题。谢谢!
来自this的回答:
The difference between check_output
and Popen
is that, while popen
is a non-blocking function (meaning you can continue the execution of the program without waiting the call to finish), check_output
is blocking.
意思是如果您正在使用 subprocess.check_output()
,您将无法获得实时输出。
尝试切换到 Popen()
。
我正在编写脚本以使用 subprocess.check_output 获取 netstat 状态。
cmd = 'netstat -nlpt'
result = subprocess.check_output(cmd, shell=True, timeout=1800)
print(result.decode('utf-8'))
以上是运行完美。有什么办法可以得到直播输出。我听说 poll() 函数可以完成这项工作。在 live output from subprocess command 他们正在使用 popen 但我正在使用 check_output 请有人帮助我解决这个问题。谢谢!
来自this的回答:
The difference between
check_output
andPopen
is that, whilepopen
is a non-blocking function (meaning you can continue the execution of the program without waiting the call to finish),check_output
is blocking.
意思是如果您正在使用 subprocess.check_output()
,您将无法获得实时输出。
尝试切换到 Popen()
。