[wxPython] wxPython and thread-safety

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] wxPython and thread-safety

Lars Marius Garshol

I've recently made an RSS viewer (that is, something that reads XML
news files from the web and presents a list of new news) using
wxPython and found wxPython to be a very nice tool for this.

Since collecting the news takes a while, and I don't want to leave the
app completely unresponsive, I've collected news in a separate thread,
and also add new list items to a list and open an extra window from
this other thread.

The list update works, but the extra window never appears when I do
the news collection in a separate thread. Also, under Linux GTK
sometimes goes crazy with GTK-CRITICAL messages.

Is wxPython thread-safe? Should I make a minimal example and submit it
as a bug report, should I use the wxWindows thread classes, or should
I just not try to do this sort of thing?

--Lars M.



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] wxPython and thread-safety

Harri Pasanen


Lars Marius Garshol wrote:

>
> I've recently made an RSS viewer (that is, something that reads XML
> news files from the web and presents a list of new news) using
> wxPython and found wxPython to be a very nice tool for this.
>
> Since collecting the news takes a while, and I don't want to leave the
> app completely unresponsive, I've collected news in a separate thread,
> and also add new list items to a list and open an extra window from
> this other thread.
>

GUI is not really thread safe, nowhere. You will have much less troubles
if you restructure your apps to non-gui worker threads that pass the
data to be displayed to the main GUI thread.

--Harri

_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] wxPython and thread-safety

Lars Marius Garshol

* Lars Marius Garshol
|
| Since collecting the news takes a while, and I don't want to leave
| the app completely unresponsive, I've collected news in a separate
| thread, and also add new list items to a list and open an extra
| window from this other thread.

* Harri Pasanen
|
| GUI is not really thread safe, nowhere. You will have much less
| troubles if you restructure your apps to non-gui worker threads that
| pass the data to be displayed to the main GUI thread.

This was more or less what I expected to hear, and it's not really a
problem to implement. Except for one thing: how do I send an event
from the non-GUI thread to the main GUI thread? Are there special
kinds of events/methods in the system that can be invoked to do this?

Thanks for the help so far. :)

--Lars M.


_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] wxPython and thread-safety

Robin Dunn
> * Lars Marius Garshol
> |
> | Since collecting the news takes a while, and I don't want to leave
> | the app completely unresponsive, I've collected news in a separate
> | thread, and also add new list items to a list and open an extra
> | window from this other thread.
>
> * Harri Pasanen
> |
> | GUI is not really thread safe, nowhere. You will have much less
> | troubles if you restructure your apps to non-gui worker threads that
> | pass the data to be displayed to the main GUI thread.
>
> This was more or less what I expected to hear, and it's not really a
> problem to implement. Except for one thing: how do I send an event
> from the non-GUI thread to the main GUI thread? Are there special
> kinds of events/methods in the system that can be invoked to do this?
>

So far the recommended procedure has been to have the worker thread add
objects to a queue which is read by the GUI thread via an EVT_IDLE handler.
You can strcuture the ocntents of the object however you want, they can be
turned into events that you fire off to a window with its ProcessEvent
method, or they can be more structured data that gets turned into specific
method calls.

I am working on a way to send events themselves (stock or user-defined) from
the worker threads that will automatically get processed in the GUI thread,
but that's not there yet.  It's waiting on a bit more support in the
wxWindows library.

--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users