Skip to main content
 首页 » 编程设计

python之Python中子进程PIPE的非阻塞读取,一次一个字节

2024年11月01日8emanlee

我在这个问题的代码上实现了一个变体:

A non-blocking read on a subprocess.PIPE in Python

尝试从这个虚拟程序中实时读取输出 test.py :

import time, sys 
 
print "Hello there" 
for i in range(100): 
    time.sleep(0.1) 
    sys.stdout.write("\r%d"%i) 
    sys.stdout.flush() 
print 
print "Go now or I shall taunt you once again!" 

另一个问题的变体是调用程序必须逐字符读取,而不是逐行读取,就像虚拟程序 test.py 一样。使用 \r 在一行上输出进度指示.所以这里是:

import sys,time 
from subprocess import PIPE, Popen 
from threading  import Thread 
 
try: 
    from Queue import Queue, Empty 
except ImportError: 
    from queue import Queue, Empty  # Python 3.x 
 
ON_POSIX = 'posix' in sys.builtin_module_names 
 
def enqueue_output(out, queue): 
    while True: 
        buffersize = 1 
        data = out.read(buffersize) 
        if not data: 
            break 
        queue.put(data) 
    out.close() 
 
p = Popen(sys.executable + " test.py", stdout=PIPE, bufsize=1, close_fds=ON_POSIX) 
q = Queue() 
t = Thread(target=enqueue_output, args=(p.stdout, q)) 
t.daemon = True # Thread dies with the program 
t.start() 
 
while True: 
    p.poll() 
    if p.returncode: 
        break 
    # Read line without blocking 
    try: 
        char = q.get_nowait() 
        time.sleep(0.1) 
    except Empty: 
        pass 
    else: # Got line 
        sys.stdout.write(char) 
        sys.stdout.flush() 
 
print "left loop" 
sys.exit(0) 

两个问题

  • 它永远不会退出 - p.returncode永远不会返回值并且不会离开循环。我该如何解决?
  • 真的很慢!有没有办法在不增加buffersize的情况下提高效率? ?

请您参考如下方法:

正如@Markku K. 指出的那样,您应该使用 bufsize=0 一次读取一个字节。

您的代码不需要非阻塞读取。你可以简化它:

import sys 
from functools import partial 
from subprocess import Popen, PIPE 
 
p = Popen([sys.executable, "test.py"], stdout=PIPE, bufsize=0) 
for b in iter(partial(p.stdout.read, 1), b""): 
    print b # it should print as soon as `sys.stdout.flush()` is called 
            # in the test.py 
p.stdout.close() 
p.wait() 

注意:一次读取 1 个字节是非常低效的。

此外,一般来说,可能有 a block-buffering issue有时可以使用 pexpect, pty modules 解决或 unbuffer, stdbuf, script command-line utilities .

对于 Python 进程,您可以使用 -u 标志强制取消缓冲(二进制层)stdin、stdout、stderr 流。