[Numpy-discussion] thread safe subclasses of ndarray

Matt Knox mattknox_ca@hotmail....
Tue Feb 6 18:54:32 CST 2007


Sturla Molden brought up a point in an earlier thread ("Please help with subclassing numpy.ndarray") that I think is worth highlighting. A "common" approach to subclassing ndarray in python (http://www.scipy.org/Subclasses) results in code that is not thread safe. This approach involves setting some class level variables inside the __new__ method, and then retrieving those values inside the __array_finalize__ method.
 
A simple work around that Sturla alluded to is to explictly set a lock inside the __new__ method, and later release it in the __array_finalize__ method. However, this would cause problems if someone invoked the __new__ method in such a way that __array_finalize__ was not called afterwards (which I think is possible, isn't it?).
 
The two subclasses of ndarray in the core numpy distribution (that I am aware of) , chararray and matrix, don't run into this problem because they can get all the relevant info they need by directly inspecting the ndarray that comes out of the __new__ method, and so they don't need to share any extra info between __new__ and __array_finalize__
 
So, I am wondering if perhaps one of the experts on this list would be able to shed some light on a good way to create thread safe subclasses of ndarray.
 
Thanks,
 
- Matt Knox
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070206/24174a87/attachment.html 


More information about the Numpy-discussion mailing list