python - 在 python 中,如何检查 subprocess.Popen 对象的标准输出是否有任何可读内容?

标签 python subprocess stdout popen readline

在 python 中,如何检查 subprocess.Popen 对象的标准输出是否有任何可读内容?我正在围绕一个有时会连续运行数小时的工具编写一个包装器。当运行时间超过几分钟时,在子进程的标准输出上使用 .readline() 会严重降低脚本的速度。如果有什么可读的,我需要一种更有效地检查标准输出的方法。顺便说一句,这个特定的工具一次只能写完整的行。脚本是这样的:

    #!/usr/bin/python -u
    #thiswrap.py

    import sys, time
    from subprocess import *

    chldp = Popen(sys.argv[1], bufsize=0, stdout=PIPE, close_fds=True)
    chstdin,chstdout=chldp.stdin,chldp.stdout
    startnoti=False

    while not chldp.poll():
        rrl=chstdout.readline() # <--- this is where the problem is
        if rrl[-8:]=='REDACTED TEXT':
            sys.stdout.write(rrl[:-1]+'   \r')
            if not startnoti: startnoti=True
        else:
            if startnoti: sys.stdout.write('\n')
            sys.stdout.write(rrl)
            if startnoti: # REDACTED
            time.sleep(0.1)
        time.sleep(0.1)

有什么想法吗?

最佳答案

你需要将文件描述符设置为非阻塞的,你可以使用fcntl来做到这一点:

import sys, time, fcntl, os
from subprocess import *

chldp = Popen(sys.argv[1], bufsize=0, stdout=PIPE, close_fds=True)
chstdin, chstdout = chldp.stdin, chldp.stdout
fl = fcntl.fcntl(chstdout, fcntl.F_GETFL)
fcntl.fcntl(chstdout, fcntl.F_SETFL, fl | os.O_NONBLOCK)

while chldp.poll() is not None:
    try:
        rrl = chstdout.readline()
    except IOError:
        time.sleep(0.1)
        continue
    # use rrl

当没有可用数据时,readline() 将引发一个IOError

请注意,由于 chldp.poll() 可以在子进程完成时返回 0,因此您可能应该使用 childp.poll() is not None 在你的 while 而不是 not childp.poll()

关于python - 在 python 中,如何检查 subprocess.Popen 对象的标准输出是否有任何可读内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6985389/

相关文章:

python - 如何从使用多处理运行脚本的 python2 子进程获取输出?

Java 运行时未捕获 Mac 上的 STDOUT

linux 标准输入、标准输出管道

Python日期字符串测试

python - TableauException (204):超时 - IPC_NamedPipe::Select(WaitForMultipleObjects)

Python 子进程标准输出被环境变量 $COLUMNS 截断

python - 如何在Python中调用程序的 'catch' stdin、stdout、stderr?

python - Keras - 如何根据一个实例进行预测?

python - 使用 Python 以编程方式检测 Windows XP 上的系统代理设置

python - 从 subprocess.run 解码 Python 中的特殊字符