1. <legend id='rl9nC'><style id='rl9nC'><dir id='rl9nC'><q id='rl9nC'></q></dir></style></legend>
      • <bdo id='rl9nC'></bdo><ul id='rl9nC'></ul>

      <i id='rl9nC'><tr id='rl9nC'><dt id='rl9nC'><q id='rl9nC'><span id='rl9nC'><b id='rl9nC'><form id='rl9nC'><ins id='rl9nC'></ins><ul id='rl9nC'></ul><sub id='rl9nC'></sub></form><legend id='rl9nC'></legend><bdo id='rl9nC'><pre id='rl9nC'><center id='rl9nC'></center></pre></bdo></b><th id='rl9nC'></th></span></q></dt></tr></i><div id='rl9nC'><tfoot id='rl9nC'></tfoot><dl id='rl9nC'><fieldset id='rl9nC'></fieldset></dl></div>

      <tfoot id='rl9nC'></tfoot>

      <small id='rl9nC'></small><noframes id='rl9nC'>

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

      时间:2023-07-20
    2. <tfoot id='MCAPH'></tfoot>

      • <i id='MCAPH'><tr id='MCAPH'><dt id='MCAPH'><q id='MCAPH'><span id='MCAPH'><b id='MCAPH'><form id='MCAPH'><ins id='MCAPH'></ins><ul id='MCAPH'></ul><sub id='MCAPH'></sub></form><legend id='MCAPH'></legend><bdo id='MCAPH'><pre id='MCAPH'><center id='MCAPH'></center></pre></bdo></b><th id='MCAPH'></th></span></q></dt></tr></i><div id='MCAPH'><tfoot id='MCAPH'></tfoot><dl id='MCAPH'><fieldset id='MCAPH'></fieldset></dl></div>

              <tbody id='MCAPH'></tbody>

                <bdo id='MCAPH'></bdo><ul id='MCAPH'></ul>

              • <legend id='MCAPH'><style id='MCAPH'><dir id='MCAPH'><q id='MCAPH'></q></dir></style></legend>

                <small id='MCAPH'></small><noframes id='MCAPH'>

                本文介绍了Python:如何以非阻塞方式读取子进程的标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试制作一个简单的 python 脚本来启动一个子进程并监视其标准输出.以下是代码片段:

                I am trying to make a simple python script that starts a subprocess and monitors its standard output. Here is a snippet from the code:

                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" 行仅在子进程终止后执行.

                The problem is that the script hangs on output=process.stdout.readline() and that the line print "test" only executes after the subprocess is terminated.

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

                Is there a way to read standard output and print it without having to wait for the subprocess to terminate?

                我要启动的子进程是一个 Windows 二进制文件,我没有它的源代码.

                The subprocess which I am starting is a Windows binary for which I do not have the source code.

                我发现了几个类似的问题,但答案仅适用于 Linux 或如果我有我要启动的 suprocess 的来源.

                I have found several similar questions, but the answers are only applicable on Linux or in case I have the source of the suprocess I am starting.

                推荐答案

                勾选select 模块

                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 系统的线程解决方案:


                Threaded Solution for non posix systems:

                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)
                

                这篇关于Python:如何以非阻塞方式读取子进程的标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:是否有 subprocess.call 的安静版本? 下一篇:Unicode 文件名到 python subprocess.call()

                相关文章

                <legend id='00b1B'><style id='00b1B'><dir id='00b1B'><q id='00b1B'></q></dir></style></legend>
                <i id='00b1B'><tr id='00b1B'><dt id='00b1B'><q id='00b1B'><span id='00b1B'><b id='00b1B'><form id='00b1B'><ins id='00b1B'></ins><ul id='00b1B'></ul><sub id='00b1B'></sub></form><legend id='00b1B'></legend><bdo id='00b1B'><pre id='00b1B'><center id='00b1B'></center></pre></bdo></b><th id='00b1B'></th></span></q></dt></tr></i><div id='00b1B'><tfoot id='00b1B'></tfoot><dl id='00b1B'><fieldset id='00b1B'></fieldset></dl></div>
              • <tfoot id='00b1B'></tfoot>

                <small id='00b1B'></small><noframes id='00b1B'>

                    <bdo id='00b1B'></bdo><ul id='00b1B'></ul>