简体   繁体   English

在多进程中无法更新 QTextEdit

[英]Can't Update QTextEdit While in a Multiprocess

I am trying to tail a file and output it continuously to a QTextEdit box.我正在尝试拖尾文件并将其连续输出到QTextEdit框。 However, I have my subprocess and output located within a multiprocess.但是,我的子进程和输出位于多进程中。 Here is my code:这是我的代码:

shouldRun = True
wMain = QtGui.QWidget()
textboxSideA = QtGui.QTextEdit(wMain)

def tailLog():
    subA = subprocess.Popen(["tail", "-F", "<FILENAME>", stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid)
    pollA = select.poll()
    pollA.register(subA.stdout)

    while shouldRun:
        if pollA.poll(1):
            textboxSideA.append(subA.stdout.readline())
    subA.kill()
    os.killpg(subA.pid, signal.SIGKILL)
    return

processSideA = multiprocessing.Process(target = tailLog)
processSideA.start()

wMain.show()

when the textboxSideA.append is called, the textbox doesn't show anything.textboxSideA.append被调用时,文本框不显示任何内容。 I have tried just appending a direct string to it just to make sure it wasn't my readline that was bad.我试过只是在它上面附加一个直接字符串,以确保它不是我的 readline 不好。 However, that wasn't the issue.然而,这不是问题。 I then tried to print out my readline directly to the terminal using print(subA.stdout.readline()) which has worked fine.然后我尝试使用print(subA.stdout.readline())将我的 readline 直接打印到终端,效果很好。 So I concluded that the QTextEdit textbox GUI isn't being updated.所以我得出的结论是 QTextEdit 文本框 GUI 没有被更新。 I have tried everything and not even Google has given me an answer.我已经尝试了一切,甚至谷歌都没有给我答案。 Also, I can type in the textbox and that shows up properly, and I can save what I have typed.此外,我可以在文本框中输入并正确显示,并且我可以保存我输入的内容。 My GUI just doesn't seem to like the multiprocess as I can call .append() outside of the multiprocess and it works just fine.我的 GUI 似乎不喜欢多.append()因为我可以在多.append()之外调用.append()并且它工作得很好。

Qt does not support multiprocessing so it is dangerous to update the GUI from another process, the GUI can only and should be updated from the thread of the process where it was created. Qt 不支持多处理,因此从另一个进程更新 GUI 是危险的,GUI 只能并且应该从创建它的进程的线程更新。

On the other hand in this case it is not necessary to use multiprocessing since you can use QProcess:另一方面,在这种情况下,没有必要使用多处理,因为您可以使用 QProcess:

import sys

from PyQt4 import QtCore, QtGui


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.process = QtCore.QProcess(self)
        self.process.setProcessChannelMode(QtCore.QProcess.MergedChannels)
        self.process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)
        self.textedit = QtGui.QTextEdit()

        self.setCentralWidget(self.textedit)

    def tail(self, filename):
        self.process.kill()
        self.process.start("tail", ["-F", filename])

    @QtCore.pyqtSlot()
    def on_readyReadStandardOutput(self):
        msg = self.process.readAllStandardOutput().data().encode()
        self.textedit.append(msg)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.tail("<FILENAME>")
    w.show()
    sys.exit(app.exec_())

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

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