0

我正在使用 PySide 在 Python 中构建一个应用程序。我有一个 GUI 可以模拟一些可以使用 Python shell 控制的硬件。我已经在一个单独的进程中启动了 GUI,它创建了自己的线程来监视两个进程之间的队列。主进程将命令放入队列,GUI 进程中的队列监视器线程阻塞,直到收到命令。队列监视器线程向 GUI 发送信号以进行更新。

问题是我的队列监视器线程没有启动。

下面是主流程中的相关代码:

def init():
    proc_comms_q_to_em = Queue()
    proc_comms_q_from_em = Queue()
    emulator = Process(
        target=run_emulator,
        args=(sys.argv, proc_comms_q_to_em, proc_comms_q_from_em))
    emulator.start()
    sleep(1)
    proc_comms_q_to_em.put(('message', 'test'))
    print("q to em size", proc_comms_q_to_em.qsize())

模拟器(GUI)过程:

class InterfaceMessageHandler(QObject):
    def __init__(self, app, q_to_em, q_from_em):
        super().__init__()
        self.main_app = app
        self.q_to_em = q_to_em
        self.q_from_em = q_from_em

    def check_queue(self):
        print("Checking queue")
        while True:
            print("Waiting for action")
            action = self.q_to_em.get()
            # emit signal so that gui gets updated


def start_interface_message_handler(
        app, emu_window, proc_comms_q_to_em, proc_comms_q_from_em):
    # need to spawn a worker thread that watches the proc_comms_q
    # need to seperate queue function from queue thread
    # http://stackoverflow.com/questions/4323678/threading-and-signals-problem
    # -in-pyqt
    intface_msg_hand_thread = QThread()
    intface_msg_hand = InterfaceMessageHandler(
        app, proc_comms_q_to_em, proc_comms_q_from_em)
    intface_msg_hand.moveToThread(intface_msg_hand_thread)
    intface_msg_hand_thread.started.connect(intface_msg_hand.check_queue)

    # connect some things to emu_window

    def about_to_quit():
        intface_msg_hand_thread.quit()
    app.aboutToQuit.connect(about_to_quit)

    intface_msg_hand_thread.start()
    print("started msg handler")


def run_emulator(
        sysargv, proc_comms_q_to_em, proc_comms_q_from_em):
    app = QApplication(sysargv)

    emu_window = EmulatorWindow()

    print("gui: Starting interface message handler")
    start_interface_message_handler(
        app, emu_window, proc_comms_q_to_em, proc_comms_q_from_em)

    print("gui: showing window")
    emu_window.show()
    app.exec_()

当我运行 init 函数时,我得到:

gui: Starting interface message handler
started msg handler
gui: showing window
q to em size 1

我的模拟器窗口出现,但我没有看到“检查队列”。

我以与此代码相同的方式启动它:https ://github.com/piface/pifacedigital-emulator/blob/testing/pifacedigital_emulator/gui.py#L442

有什么我应该注意的问题吗?这是进程/线程/QT之间通信的正确方式吗?

4

0 回答 0