Python read from subprocess stdout and stderr separately while preserving order(Python分别从子进程stdout和stderr读取,同时保留顺序)
问题描述
我有一个 python 子进程,我正在尝试从中读取输出和错误流.目前我有它的工作,但我只能在读完 stdout 之后才能从 stderr 中读取.这是它的样子:
I have a python subprocess that I'm trying to read output and error streams from. Currently I have it working, but I'm only able to read from stderr after I've finished reading from stdout. Here's what it looks like:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_iterator = iter(process.stdout.readline, b"")
stderr_iterator = iter(process.stderr.readline, b"")
for line in stdout_iterator:
# Do stuff with line
print line
for line in stderr_iterator:
# Do stuff with line
print line
如您所见,在 stdout 循环完成之前,stderr for 循环无法启动.我怎样才能修改它以便能够以正确的行进来的顺序从两者中读取?
As you can see, the stderr for loop can't start until the stdout loop completes. How can I modify this to be able to read from both in the correct order the lines come in?
澄清一下:我仍然需要能够判断一行是来自 stdout 还是 stderr 因为它们在我的代码.
To clarify: I still need to be able to tell whether a line came from stdout or stderr because they will be treated differently in my code.
推荐答案
这是一个基于 selectors 的解决方案,但它保留了顺序,并且流式传输可变长度字符(甚至是单个字符).
Here's a solution based on selectors, but one that preserves order, and streams variable-length characters (even single chars).
诀窍是使用 read1(),而不是 read().
import selectors
import subprocess
import sys
p = subprocess.Popen(
["python", "random_out.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
sel = selectors.DefaultSelector()
sel.register(p.stdout, selectors.EVENT_READ)
sel.register(p.stderr, selectors.EVENT_READ)
while True:
for key, _ in sel.select():
data = key.fileobj.read1().decode()
if not data:
exit()
if key.fileobj is p.stdout:
print(data, end="")
else:
print(data, end="", file=sys.stderr)
如果你想要一个测试程序,使用这个.
If you want a test program, use this.
import sys
from time import sleep
for i in range(10):
print(f" x{i} ", file=sys.stderr, end="")
sleep(0.1)
print(f" y{i} ", end="")
sleep(0.1)
这篇关于Python分别从子进程stdout和stderr读取,同时保留顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python分别从子进程stdout和stderr读取,同时保留顺序
基础教程推荐
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 包装空间模型 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
