thread-local data

MRAB google at mrabarnett.plus.com
Fri Feb 6 11:56:40 EST 2009


Emanuele D'Arrigo wrote:
 > Hi everybody,
 >
 > Assuming a snippet such as:
 >
 > threadLocalData = threading.local()
 > threadLocalData.myDictionary = self.myDictionary
 >
 > is it correct to say that threadLocalData.myDictionary is NOT a thread-
 > local -copy- of self.myDictionary but it's actually pointing to the
 > same object?
 >
 > If that's the case, and assuming I want to iterate over the dictionary
 > without it changing under my nose while I'm in the loop, would it be
 > better to encase the whole loop in lock-protected section or  would it
 > be better to make a copy of the dictionary first and then iterate over
 > that one? Given that in this particular thread I do not want to modify
 > the dictionary, conceptually a copy would work. But would making
 > thread-local copy be just as slow as making the whole loop thread
 > safe?
 >
It depends on how long it takes to iterate over the dict compared with
how long it takes to copy the dict. Other threads will/should be denied
access during the iteration/copying and therefore block, reducing
throughput.

I'd probably use the snapshot approach (take a copy).



More information about the Python-list mailing list