繁体   English   中英

Python:如何以非阻塞方式读取子进程的标准输出

[英]Python: How to read stdout of subprocess in a nonblocking way

我正在尝试制作一个简单的 python 脚本,该脚本启动一个子进程并监视其标准 output。 这是代码中的一个片段:

process = subprocess.Popen([path_to_exe, os.path.join(temp_dir,temp_file)], stdout=subprocess.PIPE)
while True:   
    output=process.stdout.readline()
    print "test"

问题是脚本挂在output=process.stdout.readline()上,并且print "test"行仅在子进程终止后执行。

有没有办法读取标准 output 并打印它而不必等待子进程终止?

我开始的子进程是一个 Windows 二进制文件,我没有它的源代码。

我发现了几个类似的问题,但答案仅适用于 Linux 或者如果我有我开始的 suprocess 的来源。

检查选择模块

import subprocess
import select
import time

x=subprocess.Popen(['/bin/bash','-c',"while true; do sleep 5; echo yes; done"],stdout=subprocess.PIPE)

y=select.poll()
y.register(x.stdout,select.POLLIN)

while True:
  if y.poll(1):
     print x.stdout.readline()
  else:
     print "nothing here"
     time.sleep(1)

编辑:

非posix系统的螺纹解决方案:

import subprocess
from threading import Thread 
import time

linebuffer=[]
x=subprocess.Popen(['/bin/bash','-c',"while true; do sleep 5; echo yes; done"],stdout=subprocess.PIPE)

def reader(f,buffer):
   while True:
     line=f.readline()
     if line:
        buffer.append(line)
     else:
        break

t=Thread(target=reader,args=(x.stdout,linebuffer))
t.daemon=True
t.start()

while True:
  if linebuffer:
     print linebuffer.pop(0)
  else:
     print "nothing here"
     time.sleep(1)

你可以试试这个:

import subprocess
import os

""" Continuously print command output """
""" Will only work if there are newline characters in the output. """

def run_cmd(command):    
    popen = subprocess.Popen(command, stdout=subprocess.PIPE)
    return iter(popen.stdout.readline, b"")

for line in run_cmd([path_to_exe, os.path.join(temp_dir,temp_file)]):
    print(line), # the comma keeps python from adding an empty line

从 python 3.5 开始,您可以使用os.set_blocking()如下。

import os
import subprocess

process = subprocess.Popen([path_to_exe, os.path.join(temp_dir,temp_file)], stdout=subprocess.PIPE)
os.set_blocking(process.stdout.fileno(), False) #<----- HERE
while True:   
    output=process.stdout.readline()
    print "test"


    if process.poll() is not None:
        break
rc = process.poll()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM