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

  • <small id='FomUS'></small><noframes id='FomUS'>

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

      <tfoot id='FomUS'></tfoot>

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

      1. 在子进程中运行 Pyinstaller 单个可执行输出时无法

        时间:2023-08-04
          <tbody id='2Qz7x'></tbody>
        <legend id='2Qz7x'><style id='2Qz7x'><dir id='2Qz7x'><q id='2Qz7x'></q></dir></style></legend>
      2. <i id='2Qz7x'><tr id='2Qz7x'><dt id='2Qz7x'><q id='2Qz7x'><span id='2Qz7x'><b id='2Qz7x'><form id='2Qz7x'><ins id='2Qz7x'></ins><ul id='2Qz7x'></ul><sub id='2Qz7x'></sub></form><legend id='2Qz7x'></legend><bdo id='2Qz7x'><pre id='2Qz7x'><center id='2Qz7x'></center></pre></bdo></b><th id='2Qz7x'></th></span></q></dt></tr></i><div id='2Qz7x'><tfoot id='2Qz7x'></tfoot><dl id='2Qz7x'><fieldset id='2Qz7x'></fieldset></dl></div>

              <small id='2Qz7x'></small><noframes id='2Qz7x'>

            • <tfoot id='2Qz7x'></tfoot>
                • <bdo id='2Qz7x'></bdo><ul id='2Qz7x'></ul>

                • 本文介绍了在子进程中运行 Pyinstaller 单个可执行输出时无法重定向它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我已经为此苦苦挣扎了一段时间.我已经设法编写了一个可以捕获 .py 文件的 STDOUT 的代码,但是当我使用从 Pyinstaller 生成的可执行文件运行完全相同的代码时(不管它是否窗口化)readyReadStandardOutput 信号永远不会出现.

                  I've been struggling with this for quite a while. I've managed to write a code that can capture STDOUT of .py files, however when I run the exact same code with executable generated from Pyinstaller (doesn't matter whether it's windowed or not) the readyReadStandardOutput signal doesn't ever come up.

                  根据我的测试,只有在应用程序崩溃时才会发出任何信号,但是我需要 GUI 和可执行文件之间的实时通信.

                  From my tests it occurs that any signal at all is emitted only when the app crashes, however I need a live communication between the GUI and the executable.

                  这是我的参考代码:

                    def start_process(self, program, args=None):
                          if args is None:
                              args = []
                  
                          process = QtCore.QProcess(self)
                          process.readyReadStandardOutput.connect(partial(self.onReadyReadStandardOutput, self.number_process_running))
                  
                          process.start(program)
                  

                  以及我将信号连接到的方法:

                  and the method that I've connected the signal to:

                      def onReadyReadStandardOutput(self, i):
                          print("called")
                          process = self.sender()
                          self.results[i] = process.readAllStandardOutput()
                          self.resultsChanged.emit(self.results)
                  

                  我正在使用 Windows 机器

                  I'm working on a Windows machine

                  最小的可重现示例

                  我们来写一个小脚本

                  import time
                  
                  if __name__ == "__main__":
                      num = 0
                      while True:
                          num = num + 1
                          print(num)
                          time.sleep(3)
                  

                  让我们暂时离开 PyQT 进程并使用更简单的 Popen.如果我们在 Popen 中运行脚本,stdout 将被重定向到调用进程.

                  Let's leave the PyQT processes for now and use simpler Popen. If we run the script in Popen, the stdout will be redirected to the calling process.

                  if __name__ == '__main__':
                      p = Popen([r'python', 'test.py'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
                      while p.poll() is None:
                          out = p.stdout.readline()
                          print(out)
                  

                  但是,如果我们使用第一个脚本并通过 PyInstaller 单个可执行文件生成器,Popen 将不再捕获任何输出,它只会冻结

                  However, if we take the first script and put through PyInstaller single executable generator, the Popen won't capture any output no more, it will just freeze

                  import os
                  import sys
                  
                  import PyInstaller.__main__
                  
                  file_location = os.path.dirname(os.path.realpath(__file__))
                  
                  if sys.platform == "win32":
                      PyInstaller.__main__.run([
                          '--name=%s' % 'test',
                          '--onefile',
                          '--noconsole',
                          '--distpath=%s' % os.path.join(file_location, 'dist'),
                          '--workpath=%s' % os.path.join(file_location, 'build'),
                          '--specpath=%s' % os.path.join(file_location),
                          os.path.join(file_location, 'test.py'),
                      ])
                  

                  所以我的问题是 - 我错过了其中的一些重要部分吗?或者也许这只是 pyinstaller 的错.

                  So again my question is - am I missing some important part in there? Or maybe it's just pyinstaller's fault.

                  推荐答案

                  看来你在编译脚本的时候应该把print的flush设置为True,所以改成:

                  It seems that when you compile the script you should set the flush of the print to True, so just change it to:

                  import time
                  
                  if __name__ == "__main__":
                      num = 0
                      while True:
                          num = num + 1
                          print(num, flush=True)
                          time.sleep(3)

                  这篇关于在子进程中运行 Pyinstaller 单个可执行输出时无法重定向它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:用画笔画画 下一篇:如何使用按钮打开新窗口

                  相关文章

                  最新文章

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

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