QProcess.readAllStandardOutput()好像什么也没读到

2024-03-28 10:59:14 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是代码示例:

class RunGui (QtGui.QMainWindow)

    def __init__(self, parent=None):

        ...
        QtCore.Qobject.connect(self.ui.actionNew, QtCore.SIGNAL("triggered()"), self.new_select)
        ...


    def normal_output_written(self, qprocess):
        self.ui.text_edit.append("caught outputReady signal") #works
        self.ui.text_edit.append(str(qprocess.readAllStandardOutput())) # doesn't work


    def new_select(self):
        ...
        dialog_np = NewProjectDialog()
        dialog_np.exec_()
        if dialog_np.is_OK:
            section = dialog_np.get_section()
            project = dialog_np.get_project()
            ...
            np = NewProject()
            np.outputReady.connect(lambda: self.normal_output_written(np.qprocess))
            np.errorReady.connect(lambda: self.error_output_written(np.qprocess))
            np.inputNeeded.connect(lambda: self.input_from_line_edit(np.qprocess))
            np.params = partial(np.create_new_project, section, project, otherargs)
            np.start()

class NewProject(QtCore.QThread):

    outputReady = QtCore.pyqtSignal(object)
    errorReady = QtCore.pyqtSignal(object)
    inputNeeded = QtCore.pyqtSignal(object)
    params = None
    message = ""

    def __init__(self):
        super(NewProject, self).__init__()
        self.qprocess = QtCore.QProcess()
        self.qprocess.moveToThread(self)
        self._inputQueue = Queue()

    def run(self):
        self.params()

    def create_new_project(self, section, project, otherargs):
        ...
        # PyDev for some reason skips the breakpoints inside the thread
        self.qprocess.start(command)
        self.qprocess.waitForReadyRead()
        self.outputReady.emit(self.qprocess) # works - I'm getting signal in RunGui.normal_output_written()
        print(str(self.qprocess.readAllStandardOutput())) # prints empty line
        .... # other actions inside the method requiring "command" to finish properly.

这个想法被击毙了——让GUI运行脚本并与进程通信。这个特定示例中的挑战是,在qprocessas命令中启动的脚本运行一个应用程序,该应用程序需要用户输入(确认)。因此,我必须能够启动脚本,获取所有输出并解析它,等待问题出现在输出中,然后反馈答案,允许它完成,然后才能继续执行create_new_project()中的其他操作


Tags: selfprojectuinewoutputinitdefconnect
1条回答
网友
1楼 · 发布于 2024-03-28 10:59:14

我不知道这是否能解决您的整体问题,但我在这里看到了一些设计问题。在

  1. 您将在线程之间传递qprocess,而不仅仅是使用qprocess的结果发出定制信号
  2. 您使用的类级别属性可能应该是实例属性

从技术上讲,您甚至不需要QProcess,因为您正在线程中运行它并积极使用阻塞调用。它很容易成为子流程.Popen……但无论如何,我可以建议这样的改变:

class RunGui (QtGui.QMainWindow)

    ...

    def normal_output_written(self, msg):
        self.ui.text_edit.append(msg) 

    def new_select(self):
        ...
            np = NewProject()
            np.outputReady.connect(self.normal_output_written)
            np.params = partial(np.create_new_project, section, project, otherargs)
            np.start()

class NewProject(QtCore.QThread):

    outputReady = QtCore.pyqtSignal(object)
    errorReady = QtCore.pyqtSignal(object)
    inputNeeded = QtCore.pyqtSignal(object)

    def __init__(self):
        super(NewProject, self).__init__()

        self._inputQueue = Queue()
        self.params = None

    def run(self):
        self.params()

    def create_new_project(self, section, project, otherargs):
        ...
        qprocess = QtCore.QProcess()
        qprocess.start(command)
        if not qprocess.waitForStarted():
            # handle a failed command here
            return

        if not qprocess.waitForReadyRead():
            # handle a timeout or error here
            return

        msg = str(self.qprocess.readAllStandardOutput())
        self.outputReady.emit(msg) 

不要传递QProcess。只是发出数据。并从threads方法中创建它,以便它自动归该线程所有。您的外部类实际上不应该了解QProcess对象。它甚至不需要是成员属性,因为它只在操作期间需要。在

还要确保正确地检查命令是否成功启动,以及是否正在运行和输出数据。在

更新

为了澄清您可能遇到的一些问题(根据注释),我想建议,如果您需要与期望定期用户输入的进程进行交互控制,那么QProcess可能不是最佳选择。虽然真正使用子流程会更容易,但它应该可以用于运行从开始到结束只产生输出的脚本。对于需要用户长期输入的脚本,最好使用^{}。它允许您生成一个进程,然后观察您知道的将指示需要输入的各种模式:

食品

^{2}$

测试.py

import pexpect
import time

child = pexpect.spawn("python foo.py")
child.setecho(False)

ret = -1
while ret < 0:
    time.sleep(.05)
    ret = child.expect("Please enter something: ")

child.sendline('FOO')
while True:
    line = child.readline()
    if not line:
        break
    print line.strip()

# Output: FOO
# Another line
# Done

相关问题 更多 >