Multiple threads in a GUI app (wxPython), communication between worker thread and app?
Jp Calderone
exarkun at divmod.com
Sun May 1 13:46:07 EDT 2005
On 01 May 2005 10:09:56 -0700, Paul Rubin <"http://phr.cx"@nospam.invalid> wrote:
>"fooooo" <phark52 at yahoo.com> writes:
>> How would I get the worker thread to open a GUI window in the main GUI
>> thread? After that GUI window is open, how can I send and recv messages
>> from/to the GUI window?
>
>First of all the favorite Pythonic way to communicate between threads
>is with synchronized queues--see the Queue module. Have the worker
>thread put stuff on a queue and have the main GUI thread read from it.
Yea, this is a good way. Also, it is essentially what you describe below :) The Queues are just created and drained by the GUI library instead of the user's application code.
>
>Secondly, I don't know about wxPython, but in tkinter you have to
>resort to a kludge in order for the gui thread to handle gui events
>and also notice stuff on a queue. There's a tkinter command to run
>some function after a specified time (say 50 msec). So you'd set that
>timeout to check the queue and restart the timer, which means the gui
>would check 20x a second for updates from the worker threads. When it
>got such an update, it would create a new window or whatever.
You can do better than this. Tkinter has an after_idle function, which lets you post an event to the Tkinter thread _immediately_. This is basically the Queue "put" operation.
>
>It could be that wxPython has a cleaner way of doing this, or you
>might have to do something similar.
It has essentially the same thing, PostEvent().
> Python thread support seems to have been something of an afterthought
> and there's a lot of weirdness like this to deal with.
I'm not sure what you see as weird about this.
Jp
More information about the Python-list
mailing list