How to implement something like wx.CallAfter... in the other direction?

Hi guys,

I've made good use of wx.CallAfter to allow a worker thread to call
quite arbitrary wx functions back in the GUI thread, but to date I've
always used very simple-minded techniques to communicate from the GUI
Thread back to the worker thread. (E.g., setting or clearing simple
variables with the assumption that their access is atomic.)

Is there a straightforward way to make something as flexible as
wx.CallAfter that would allow GUI to worker communication? My command
of Python isn't advanced enough to understand how a function can queue
up an arbitrary number of passed parameters and then provide them back
to a different thread.

I take it wx.CallAfter's dispatch must actually come from wx's main
event loop, and therefore a worker thread would have also specifically
check for queued commands regularly, when it was otherwise just going
to sleep, etc.

Thanks!

---Joel

Joel Koltner wrote:

Hi guys,

I've made good use of wx.CallAfter to allow a worker thread to call
quite arbitrary wx functions back in the GUI thread, but to date I've
always used very simple-minded techniques to communicate from the GUI
Thread back to the worker thread. (E.g., setting or clearing simple
variables with the assumption that their access is atomic.)

Is there a straightforward way to make something as flexible as
wx.CallAfter that would allow GUI to worker communication? My command
of Python isn't advanced enough to understand how a function can queue
up an arbitrary number of passed parameters and then provide them back
to a different thread.

I take it wx.CallAfter's dispatch must actually come from wx's main
event loop, and therefore a worker thread would have also specifically
check for queued commands regularly, when it was otherwise just going
to sleep, etc.

Thanks!

One way would be to use python's Queue module.

Set up a Queue.Queue instance at the top of the program, so that it is
visible to both threads. Alternatively create it in one thread and pass it
as a parameter to the other one.

When wx wants to communicate with the worker thread, it can 'put' something
onto the queue.

The worker thread can check if the queue is empty, and if not, 'get' the
item and deal with it.

HTH

Frank Millman