我有一个应用程序,有一个长时间运行的后台进程(daemon)和一个基于wx的UI。后台进程将消息放入多处理.JoinableQueue()UI应该从中读取,以便在守护进程运行时获取状态更新。我遇到的问题是,当AwaitStatusReportThreadClass实例将消息传递给UI时,它会导致UI停止响应。如果运行下面的代码,然后尝试调整窗口大小,应用程序将显示为没有响应。你知道吗
我使用Python27 32位,wxpython4.0.0a3(Phoenix)在windows7上运行这个程序
我的应用程序是用各种python文件构建的,但是我能够用下面的代码重现错误。这很简单。我创建了一个名为AwaitStatusReportThreadClass的类来使用here描述的pub/sub方法来更新UI。此类的init方法有一个检查状态消息的无限循环。当找到一个时,它会更新状态栏消息。你知道吗
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()
有什么建议吗?你知道吗
嗯,解决办法很简单。我把它留在这里,以防将来对任何人都有帮助。你知道吗
所需要的只是重新构造要使用的AwaitStatusReportThreadClasswx.CallAfter公司发布消息。我向类中添加了postMessage函数,并使用
wx.CallAfter(self.postMessage, msg)
调用它相关问题 更多 >
编程相关推荐