Managing thread communication with the queue object

Although using CallAfter() is the easiest way to manage thread communication, it's not the only mechanism. You can use Python's thread-safe Queue object to send command objects to the UI thread. The UI thread should be written to take commands from this queue in a wx.EVT_lDLE event handler.

Essentially, you will be setting up a parallel event queue for thread communication. The command objects can be whatever makes sense for the application, ranging from simple data values to thread objects whose run() method should be called when invoked. If you are using this method, the worker threads should call the global function wx.WakeUpldle() when they add a command object to the queue to ensure that there will be an idle event as soon as possible. This technique is more complex than wx.CallAfter(), but it's a lot more flexible. In particular, this mechanism can help you to communicate between two different background threads, although all GUI manipulation should still be on the main thread.

Was this article helpful?

0 0

Post a comment