为什么在pyinstaller完成.exe之后,子进程的所有标准都被一次性打印到QTextBrower?

Pycharm可以正确运行我的代码,将子进程stdout逐个打印子进程stdout到qt控件(QTextBrower),但是在pyinstaller打印子进程.exe之后,它会一次打印所有标准输出,直到子进程完成,这并不是一个预期的结果

我尝试在子进程中使用flush()和stdout.close,仍然是一样的。

NonBlockingStreamReader类:

def __init__(self, stream):
    self._s = stream
    self._q = Queue()

    def _populateQueue(stream, queue):           
        while True:
            line = stream.readline()
            if line:
                queue.put(line)
            #else:
                #raise UnexpectedEndOfStream

    self._t = Thread(target = _populateQueue, args = (self._s, self._q))
    self._t.daemon = True
    self._t.start() #start collecting lines from the stream

def readline(self, timeout=None):
    try:
        return self._q.get(block=timeout is not None, timeout=timeout)

    except Empty:
        return None

.

form =uic.loadUiType(“数据/图形用户界面/GUI.ui”)

类表单(QtGui.QDialog,表单):

def __init__(self, parent=None):

    QtGui.QDialog.__init__(self, parent)
    self.setupUi(self)
    os.chdir("../../")       
    self.LogAnalyzeButton.clicked.connect(self.LogAnalyzePre)

.

def LogAnalyzePre(self):
    self.Console.append("Analyzing log, please wait . . . . . . ." + "\n" )
    arguments = 'python log.py %s'%(path)
    self.proc = Popen(arguments, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
    nbsr = NonBlockingStreamReader(self.proc.stdout)
    while self.proc.poll() is None:
        line = nbsr.readline(0.1)
        print line
        if line:
            self.Console.insertPlainText(unicode(line, "utf-8"))
            self.Console.moveCursor(QtGui.QTextCursor.End)
        QtGui.QApplication.processEvents()

在运行.exe时,我可以看到debug cmd窗口显示该行的值始终为None,并且在子进程关闭之前,队列中的stdouts会立即打印出来

转载请注明出处:http://www.jixiangdc.com/article/20230526/1129523.html