我正在用PySide用Python构建一个应用程序。我有一个GUI,它模拟一些可以使用pythonshell控制的硬件。我已经在一个单独的进程中启动了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())
emulator(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之间通信的正确方式吗?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐