wxPython 中的观察者模式
我正在尝试使用 wxPython 实现观察者设计模式。 我有一个建模应用程序,可以在后台计算大量数据。有时我想在 GUI 中显示模型的输出——它只是不同颜色的正方形网格。其他时候我需要在不显示 GUI 的情况下进行计算。
观察者模式的优点是,您只需添加或删除一行代码(类似
self.observers.append(MyWxGui())
或类似的代码)即可插入或不插入 GUI。 现在,要做到这一点,我需要我的计算在一个线程上运行,而 wx GUI 在另一个线程中运行。 我尝试使用 wxPython 执行此操作,但总是遇到致命 I/O 错误:
python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0.
我阅读了有关 wxPython 中多线程的教程,例如 http://wiki.wxpython.org/LongRunningTasks,但它们都有在主线程中运行的 Mainloop() ,而不是在辅助线程中运行长时间的任务,而我需要它是相反的。这是因为如果我在主线程中有 Mainloop(),程序就会挂起等待来自 GUI 的某些事件,而不是继续进行计算。
我还发现我无法在子线程中操作设备上下文 (DC),例如 ClientDC 或 PaintDC,但我在同一线程内运行整个 wx 代码。
Mainloop() 和所有 wx GUI 是否可以在其自己的线程(不是主应用程序的线程)中运行?
在 Ubuntu 10.10 maverick 上运行 wxPython 2.8.11.0。
I am trying to implement the Observer design pattern with wxPython.
I have a modelling application that computes vast amount of data in the background. Sometimes I would like to display the output of the model in the GUI---which is just a grid of squares of different colours. Other times I need to do the computation without displaying the GUI.
The advantage of the observer pattern is that you can plug in or not a GUI just by adding or removing one line of code, something like
self.observers.append(MyWxGui())
or similar.
Now, to do that I need my computation to run on one thread, and the wx GUI to run in a different one.
I tried doing this with wxPython but I always get a Fatal I/O error:
python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0.
I read tutorials on multithreading in wxPython, such as http://wiki.wxpython.org/LongRunningTasks, but they all have the Mainloop() running in the main thread and than the long running task in a secondary thread, while I need it to be the other way round. This is because if I have the Mainloop() in the main thread, the program hangs waiting for some event from the GUI, instead of proceeding with the computation.
I also saw that I cannot manipulate Device contexts (DCs) such as ClientDC or PaintDC in a sub-thread, but I'm running the entire wx code inside the same thread.
Can the Mainloop() and all the wx GUI be run in its own thread that is not the main application's one?
Running wxPython 2.8.11.0 on Ubuntu 10.10 maverick.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您阅读过该 wiki 页面,那么您应该知道您可以使用 wx.CallAfter、wxCallLater 或 wx.PostEvent 以线程安全的方式与 wx 线程进行通信。我这里有一个简单的教程:
http://www.blog .pythonlibrary.org/2010/05/22/wxpython-and-threads/
就个人而言,我会使用类似 Pubsub + 上面提到的线程安全方法之一来与 wx MainLoop 进行通信。 Pubsub 的好处是它可以监听消息并做出适当的反应。上面的示例实际上展示了一种实现此目的的方法。希望这会对您有所帮助。否则,我强烈建议加入 wxPython 邮件列表并在那里提问: http://groups.google.com/group/wxpython-users/topics?pli=1" rel="nofollow">http:// /groups.google.com/group/wxpython-users/topics?pli=1
If you read that wiki page, then you should know that you can communicate back to the wx thread using wx.CallAfter, wxCallLater or wx.PostEvent in a thread-safe manner. I have a simple tutorial here:
http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
Personally, I would use something like Pubsub + one of the threadsafe methods mentioned above to communicate with the wx MainLoop. The nice thing about Pubsub is that it can listen for messages and react to them appropriately. The example above actually shows one way to do just that. Hopefully that will help you. Otherwise, I highly recommend joining the wxPython mailing list and asking there: http://groups.google.com/group/wxpython-users/topics?pli=1