[wxPython-users] Can a background thread...?

Tal Einat taleinat at gmail.com
Tue Jul 31 13:13:13 PDT 2007


On 7/31/07, Gre7g Luterman <hafeliel at yahoo.com> wrote:
> Thanks for the guidelines, Robin.  I guess no one has
> any hard rules on which calls are safe and which
> aren't.
>
> While I'm on the subject, does anyone have a trick or
> method they use to keep their thread-safe code
> separate from the non-thread-safe?  i.e. I make a
> conscious effort to put "Thread" in a function name if
> it gets launched as a background thread, but I'll be
> the first to admit that my organization stops there.
> If those background threads call another function, I'm
> in no position to know for a fact whether that
> function can run legally in a background thread.
>
> I guess the only solution to this danger is a
> programmer's self control (which I seem to lack), so
> how do you guys keep from falling into the sloppy-code
> trap here?

I usually do one of two things:

1) Avoid threads when possible, use threads only for non-GUI-related
things. I usually find that most GUI related problems that threads can
solve can also be solved with timers and such, so even if it makes the
code slightly more complex, it saves me threading headaches. (like
Christopher said)

2) Completely de-couple the GUI from the logic - turn the main thread
into a GUI handler running in a loop, and have it communicate with the
rest of the application via Queues or events or some such. This could
take a bit of "practice" to get right, but it isn't actually very
complicated. There have been discussions on how to do this on this
list before, so try a search if you're interested.

Just my 2 cents,
- Tal




More information about the wxpython-users mailing list