问题描述
我有一个 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读取,同时保留顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!