[wxPython-users] Re: threading

Josiah Carlson jcarlson at uci.edu
Mon Mar 19 19:24:32 PDT 2007


Christian <ckkart at hoc.net> wrote:
> 
> Hi Josiah,
> 
> Josiah Carlson wrote:
> > Christian <ckkart at hoc.net> wrote:
> >> I tried to move some heavy calulations to a separate thread as dfemonstrated on
> >> the LongRunningTasks wiki page. I did that by passing a reference to the method
> >> which does the calculation to the Thread class on creation and have it called
> >> from the Thread's run method. Is that allowed when threading?
> > 
> > Presumably you mean that you used...
> > 
> >     x = threading.Thread(target=obj.method, args=...)
> >     x.start()
> >
> > or some variant thereof.  Yes, that should work just fine.  You may need
> 
> Almost.
[snip]
>     def run(self):
>         msg = self._fitter.run()
>         event = pevent.ResultEvent(self._notify.GetId(), msg)
>         self._notify.GetEventHandler().ProcessEvent(event)
> 
> _fitter.run() is directly modifying data of the gui thread.

Just Python data structures (list, dict, etc.), or GUI widgets?  If GUI
widgets; stop that.  wxPython doesn't behave well if you are accessing
GUI objects in a secondary thread.

Also, you may want to consider making your worker thread daemonic, so if
your GUI closes down, your thread disappears automatically, rather than
keeping your application alive until that worker finishes (which then
results in an exception because the GUI widgets it is trying to post an
event to has been destroyed).


 - Josiah





More information about the wxpython-users mailing list