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

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

      1. <legend id='HqyKp'><style id='HqyKp'><dir id='HqyKp'><q id='HqyKp'></q></dir></style></legend><tfoot id='HqyKp'></tfoot>

        实时拦截子流程输出的问题

        时间:2023-07-21

                <tbody id='yxpVA'></tbody>

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

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

                  <tfoot id='yxpVA'></tfoot><legend id='yxpVA'><style id='yxpVA'><dir id='yxpVA'><q id='yxpVA'></q></dir></style></legend>
                • <i id='yxpVA'><tr id='yxpVA'><dt id='yxpVA'><q id='yxpVA'><span id='yxpVA'><b id='yxpVA'><form id='yxpVA'><ins id='yxpVA'></ins><ul id='yxpVA'></ul><sub id='yxpVA'></sub></form><legend id='yxpVA'></legend><bdo id='yxpVA'><pre id='yxpVA'><center id='yxpVA'></center></pre></bdo></b><th id='yxpVA'></th></span></q></dt></tr></i><div id='yxpVA'><tfoot id='yxpVA'></tfoot><dl id='yxpVA'><fieldset id='yxpVA'></fieldset></dl></div>
                  本文介绍了实时拦截子流程输出的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在堆栈溢出上花了大约 6 个小时,重写了我的 python 代码并试图让它工作.它只是没有.不管我做什么.

                  I've spent about 6 hours on stack overflow, rewriting my python code and trying to get this to work. It just doesn't tho. No matter what I do.

                  目标:让子进程的输出实时显示在 tkinter 文本框中.

                  The goal: Getting the output of a subprocess to appear in real time in a tkinter text box.

                  问题:我不知道如何使 Popen 实时工作.它似乎挂起,直到该过程完成.(自行运行,过程完全按预期运行,所以只是这个东西有错误)

                  The issue: I can't figure out how to make the Popen work in real time. It seems to hang until the process is complete. (Run on its own, the process works completely as expected, so it's just this thing that has the error)

                  相关代码:

                  import os
                  import tkinter
                  import tkinter.ttk as tk
                  import subprocess
                  
                  class Application (tk.Frame):
                      process = 0
                      def __init__ (self, master=None):
                          tk.Frame.__init__(self, master)
                          self.grid()
                          self.createWidgets()
                  
                      def createWidgets (self):
                          self.quitButton = tk.Button(self, text='Quit', command=self.quit)
                          self.quitButton.grid()
                          self.console = tkinter.Text(self)
                          self.console.config(state=tkinter.DISABLED)
                          self.console.grid()
                  
                      def startProcess (self):
                          dir = "C:/folder/"
                          self.process = subprocess.Popen([ "python", "-u", dir + "start.py" ], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, cwd=dir)
                          self.updateLines()
                  
                      def updateLines (self):
                          self.console.config(state=tkinter.NORMAL)
                          while True:
                              line = self.process.stdout.readline().decode().rstrip()
                              if line == '' and self.process.poll() != None:
                                  break
                              else: 
                                  self.console.insert(tkinter.END, line + "
                  ")
                          self.console.config(state=tkinter.DISABLED)
                          self.after(1, self.updateLines)
                  
                  app = Application()
                  app.startProcess()
                  app.mainloop()
                  

                  另外,如果我的代码写得不好,请随意销毁我的代码.这是我的第一个 python 项目,我不希望对语言有任何好处.

                  Also, feel free to destroy my code if it's written poorly. This is my first python project, I don't expect to be any good at the language yet.

                  推荐答案

                  这里的问题是 process.stdout.readline() 会阻塞,直到有完整的行可用.这意味着条件 line == '' 在进程退出之前永远不会满足.您有两种选择.

                  The problem here is that process.stdout.readline() will block until a full line is available. This means the condition line == '' will never be met until the process exits. You have two options around this.

                  首先,您可以将 stdout 设置为非阻塞并自己管理缓冲区.它看起来像这样.正如 Terry Jan Reedy 指出的,这是一个仅适用于 Unix 的解决方案.应该首选第二种选择.

                  First you can set stdout to non-blocking and manage a buffer yourself. It would look something like this. As Terry Jan Reedy pointed out this is a Unix only solution. The second alternative should be preferred.

                  import fcntl
                  ...
                  
                      def startProcess(self):
                          self.process = subprocess.Popen(['./subtest.sh'],
                              stdout=subprocess.PIPE,
                              stdin=subprocess.PIPE,
                              stderr=subprocess.PIPE,
                              bufsize=0) # prevent any unnecessary buffering
                  
                          # set stdout to non-blocking
                          fd = self.process.stdout.fileno()
                          fl = fcntl.fcntl(fd, fcntl.F_GETFL)
                          fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
                  
                          # schedule updatelines
                          self.after(100, self.updateLines)
                  
                      def updateLines(self):
                          # read stdout as much as we can
                          line = ''
                          while True:
                              buff = self.process.stdout.read(1024)
                              if buff:
                                  buff += line.decode()
                              else:
                                  break
                  
                          self.console.config(state=tkinter.NORMAL)
                          self.console.insert(tkinter.END, line)
                          self.console.config(state=tkinter.DISABLED)
                  
                          # schedule callback
                          if self.process.poll() is None:
                              self.after(100, self.updateLines)
                  

                  第二种选择是让一个单独的线程将行读入队列.然后从队列中弹出更新行.它看起来像这样

                  The second alternative is to have a separate thread read the lines into a queue. Then have updatelines pop from the queue. It would look something like this

                  from threading import Thread
                  from queue import Queue, Empty
                  
                  def readlines(process, queue):
                      while process.poll() is None:
                          queue.put(process.stdout.readline())
                  ...
                  
                      def startProcess(self):
                          self.process = subprocess.Popen(['./subtest.sh'],
                              stdout=subprocess.PIPE,
                              stdin=subprocess.PIPE,
                              stderr=subprocess.PIPE)
                  
                          self.queue = Queue()
                          self.thread = Thread(target=readlines, args=(self.process, self.queue))
                          self.thread.start()
                  
                          self.after(100, self.updateLines)
                  
                      def updateLines(self):
                          try:
                              line = self.queue.get(False) # False for non-blocking, raises Empty if empty
                              self.console.config(state=tkinter.NORMAL)
                              self.console.insert(tkinter.END, line)
                              self.console.config(state=tkinter.DISABLED)
                          except Empty:
                              pass
                  
                          if self.process.poll() is None:
                              self.after(100, self.updateLines)
                  

                  线程路由可能更安全.我不确定将 stdout 设置为非阻塞是否适用于所有平台.

                  The threading route is probably safer. I'm not positive that setting stdout to non-blocking will work on all platforms.

                  这篇关于实时拦截子流程输出的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:通过 subprocess.check_output 调用的可执行文件在控制台上打印,但不返回结果 下一篇:Python ClearCase 下载 Vobs Popen 密码 BASH 程序 Sketchy

                  相关文章

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

                      <tfoot id='FlGTQ'></tfoot>

                    1. <legend id='FlGTQ'><style id='FlGTQ'><dir id='FlGTQ'><q id='FlGTQ'></q></dir></style></legend>

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

                      • <bdo id='FlGTQ'></bdo><ul id='FlGTQ'></ul>