Threadsafe Discipline

Hey Listies – I’m looking for general advice on how best to keep my threadsafe code isolated from my non-threadsafe code.

[…]

I’m not real clear on which wxPython calls are threadsafe, so I’m trying to make an effort not to call any of them (even stuff like wx.GetApp(), which I presume must be threadsafe).

As a rule of thumb, I assume that only wx.CallAfter() is thread-safe.

[…]

My Compile() function, however, is a lot more real-worldy than this. It calls other member functions and they call other member functions. To be able to guarantee that Compile() is threadsafe, I need to not only make sure there are no wx.* calls in it, but in every function it calls (and they call, and so forth). I should even make sure that I don’t pass in objects to Compile() which might have non-threadsafe members. Doing so could easily lead to a non-safe call even if Compile() and its cronies are written correctly.

Put your compile-function in a seperate module (a seperate .py file), with all of the member and helper functions it needs. If it’s a method of a class, the whole class goes there.

From this module, and any other modules that need to be threadsafe, you do not import wx or anything wx - related.
That is how you’re sure, 100% certain, that they don’t make any unwanted calls to the GUI.

In order to give feedback and update the GUI, you pass in a callback-method to your Compile() function. This method ‘lives’ in the GUI module, and is written such that it only calls wx.CallAfter – any other GUI work is done from the method called by wx.CallAfter (the code executed by CallAfter is executed in the Main Thread).

I hope this is clear; it seems to match your own strategy.

(If you make it a sub-directory btw, don’t forget to make it a package, otherwise you cannot import it: you make a file ‘init.py’ in the directory, it can be empty. See Python docs.)

[…]

Cheers,

–Tim

···

On Mon, Apr 21, 2008 at 10:43 PM, Gre7g Luterman gre7g.luterman@gmail.com wrote:

TIA,

Gre7g


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Tim van der Leeuw wrote:

     I'm not real clear on which wxPython calls are threadsafe, so I'm
    trying to make an effort not to call any of them (even stuff like
    wx.GetApp(), which I presume must be threadsafe).

As a rule of thumb, I assume that only wx.CallAfter() is thread-safe.

For the record, so is wx.PostEvent, wx.WakeUpIdle, wx.EvtHandler.AddPendingEvent and probably a few others. You can also create event objects in the non-gui thread for use in passing to these functions, as well as things like wx.Point, wx.Size, wx.Rect, etc.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!