Python 分别从子进程 stdout 和 stderr 读取,同时保留顺序 [英] Python read from subprocess stdout and stderr separately while preserving order

查看:58
本文介绍了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 读取,同时保留顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆