wxPython and threads again

Bryan Olson fakeaddress at nowhere.org
Wed Aug 10 20:00:32 EDT 2005


Peter Hansen wrote:
 > David E. Konerding DSD staff wrote:
 >> Further, calling wx from a thread other than the one running the
 >> event loop is deep voodoo and should typically be avoided.
 >
 > "Typically"?  Let's just say "always" and maybe use the phrase "certain
 > to corrupt wx and crash the app" instead of "deep voodoo". :-)  At least
 > that way the OP won't waste time experimenting...

Come to think of it, wouldn't it be a good idea for a GUI
toolkit to to do something like:

     import thread

     # ...

     def WhateverToolKitInitFunction(*args):
         global _thread_of_record
         _thread_of_record = thread.get_ident()
         # ...


     def check_thread():
         if thread.get_ident() != _thread_of_record:
             raise RuntimeError('Attempt to update GUI from foreign 
thread.')


And then begin each non-thread-safe function like:


     def SomeUpdateFunction(*args):
         check_thread()
         # ...


[...]
 > Even simpler for some purposes is wx.CallAfter(),

Ah, Nice. Same method under the hood, but hides the complexity.


-- 
--Bryan



More information about the Python-list mailing list