您了解这种僵局吗? [英] Do you understand this deadlock?
问题描述
我将wxPython用于我的GUI.在AppLogic
类中,我有一个工作线程,该线程在该类的方法中运行.
I'm using wxPython for my GUI. In the AppLogic
class I have a worker thread that is run in a method of this very class.
这是GUI
类:
class GUI:
_wx_app = None
_main_window = None
_app_logic = None
def start(self):
# bla bla bla
self._main_window.Show()
self._app_logic.begin()
self._wx_app.MainLoop()
def _cancel_listener(self):
"""Called from MainWindow, if the user has clicked the cancel
button."""
print("I'm leaving this scope.")
self._app_logic.cancel() # Should cancel the task done in
# a second thread.
def _status_listener(self, status_text):
"""Called often by the worker thread."""
print("Here I am again.")
if self._main_window.status.GetLabel() != status_text:
self._main_window.status.SetLabel(status_text)
这是AppLogic
类中的cancel方法,由_cancel_listener
从上面调用:
Here's the cancel method from the AppLogic
class which is called by _cancel_listener
from above:
def cancel(self):
self._shall_abort = True
self._thread.join(self._some_time_out)
assert self._thread.isAlive() == False
不知何故,其中涉及到join
和GetLabel
(因此也包括MainLoop
?)陷入了僵局,但我并不真正了解发生了什么.有人对此有更深入的了解吗?那就太好了!
Somehow, there's a deadlock with join
and GetLabel
(and thus MainLoop
?) involved, but I don't really understand what's going on. Does someone have more insight into this? That would be great!
推荐答案
所有GUI工具包都有一个主GUI线程.它们都有特殊的方法,可让您以线程安全的方式操作GUI小部件.在wxPython世界中,这些方法是wx.CallAfter,wx.CallLater和wx.PostEvent.我在您的示例中没有看到这些内容,因此您所做的基本上是暂停GUI线程或导致发生未定义"事件.
All GUI toolkits have a main GUI thread. They all have special methods that allow you to manipulate the GUI widgets in a thread-safe way. In wxPython world, those methods are wx.CallAfter, wx.CallLater and wx.PostEvent. I don't see those anywhere in your example, so what you're doing is basically pausing the GUI thread or causing something "undefined" to occur.
以下是有关线程和wxPython的几篇文章:
Here are a couple of articles on threads and wxPython:
- http://wiki.wxpython.org/LongRunningTasks
- http://www.blog.pythonlibrary.org /2010/05/22/wxpython-and-threads/
- http://wiki.wxpython.org/LongRunningTasks
- http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
这篇关于您了解这种僵局吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!