我正在使用PySide在Python中构建应用程序。我有一个GUI模拟一些可以使用Python shell控制的硬件。我在一个单独的进程中启动了GUI,它创建了自己的线程来监视两个进程之间的队列。主进程将命令放在GUI Process块中的队列和队列监视器线程中,直到收到命令为止。队列监视器线程将信号发送到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之间通信的正确方法吗?