我的应用程序具有长时间运行的后台(守护程序)进程和基于wx的UI。后台进程将消息放入多处理.JoinableQueue()中,UI应该从中读取以在守护进程运行时获取状态更新。我遇到的问题是当AwaitStatusReportThreadClass实例将消息传递给UI时,它会导致UI停止响应。如果您运行下面的代码,然后尝试调整窗口大小,则应用程序显示为无响应。
我在Windows 7上使用Python27 32bit,wxPython 4.0.0a3(Phoenix)运行它
我的应用程序使用各种python文件构建,但我能够使用以下代码重现错误。这很简单。我创建了一个名为AwaitStatusReportThreadClass的类,以使用描述here的pub / sub方法来更新UI。此类的init方法具有无限循环,用于检查状态消息。找到一个后,它会更新StatusBar消息。
import multiprocessing
import threading
import wx
import sys
from wx.lib.pubsub import pub
class AwaitStatusReportThreadClass(threading.Thread):
"""This class should pass messages to the UI class"""
def __init__(self, messageQueue):
"""Init worker Thread Class"""
self.messageQueue = messageQueue
threading.Thread.__init__(self)
self.start()
def run(self):
"""This code executes when the thread is run"""
KeepRunningStatusThread = True
while KeepRunningStatusThread:
sys.stdout.flush()
try:
msg = self.messageQueue.get()
self.messageQueue.task_done()
except:
pass
if msg == "Shutdown":
# Kill this thread
KeepRunningStatusThread = False
else:
pub.sendMessage("UI", msg=msg)
class UI2(wx.Frame):
"""This class is the UI"""
def __init__(self, taskQueue, messageQueue, stopQueue, parent=None):
self.taskQueue = taskQueue
self.messageQueue = messageQueue
self.stopQueue = stopQueue
wx.Frame.__init__(self, parent, title="TestApp")
#Main panel
sizerMain = wx.BoxSizer(wx.VERTICAL)
# Add the status bar
panelStatusBar = wx.Panel(self)
sizerStatusBar = wx.BoxSizer(wx.HORIZONTAL)
panelStatusBar.SetSizer(sizerStatusBar)
self.StatusBar_Main = wx.StatusBar(panelStatusBar, wx.NewId())
sizerStatusBar.Add(self.StatusBar_Main, 1, wx.EXPAND | wx.LEFT | wx.RIGHT, 2)
#Add the status bar sizer to the main sizer
sizerMain.Add(panelStatusBar, 0, wx.EXPAND)
#Add the progress bar
panelProgressBar = wx.Panel(self)
sizerProgressBar = wx.BoxSizer(wx.HORIZONTAL)
panelProgressBar.SetSizer(sizerProgressBar)
self.Gauge_ProgressBar = wx.Gauge(panelProgressBar, wx.NewId())
sizerProgressBar.Add(self.Gauge_ProgressBar, 1, wx.EXPAND)
sizerMain.Add(panelProgressBar,0,wx.EXPAND)
#Layout the frame
self.SetSizer(sizerMain)
self.SetAutoLayout(1)
sizerMain.Fit(self)
self.Layout()
self.Show(show=True)
#Subscribe to messages from the messageQueue
pub.subscribe(self.HandleStatusUpdate, "UI")
AwaitStatusReportThreadClass(self.messageQueue)
def HandleStatusUpdate(self, msg):
"""
This def updates the UI from a pubsub subscription
"""
StatusBar = self.StatusBar_Main
StatusBar.PushStatusText(msg)
if __name__ == "__main__":
#Make multiprocessing work when app is frozen
multiprocessing.freeze_support()
taskQueue = multiprocessing.JoinableQueue() #Specifies tasks to be completed by the GenInst process
messageQueue = multiprocessing.JoinableQueue() #Holds status messages / progress messages to update the message zone and progress bar in the UI
stopQueue = multiprocessing.JoinableQueue() #Allows cancel operation button to function
messageQueue.put("Please wait while the GenInst background process starts...")
#Start the UI
app = wx.App(False)
frame = UI2(taskQueue=taskQueue, messageQueue=messageQueue, stopQueue=stopQueue)
app.MainLoop()
有什么建议吗?
答案 0 :(得分:0)
嗯,解决方案很简单。我会把它留在这里以防将来帮助任何人。
所需要的只是重构AwaitStatusReportThreadClass以使用wx.CallAfter发布消息。我在类中添加了一个postMessage函数,并使用wx.CallAfter(self.postMessage, msg)
class AwaitStatusReportThreadClass(threading.Thread):
"""This class should pass messages to the UI class"""
def __init__(self, messageQueue):
"""Init worker Thread Class"""
self.messageQueue = messageQueue
threading.Thread.__init__(self)
self.start()
def run(self):
"""This code executes when the thread is run"""
KeepRunningStatusThread = True
while KeepRunningStatusThread:
sys.stdout.flush()
try:
msg = self.messageQueue.get()
self.messageQueue.task_done()
wx.CallAfter(self.postMessage, msg)
except:
pub.sendMessage("Failed")
def postMessage(self, msg):
pub.sendMessage("UI", msg=msg)