Is defaultdict thread safe?
Raymond Hettinger
python at rcn.com
Mon Jan 25 04:26:52 EST 2010
On Jan 25, 12:59 am, "Frank Millman" <fr... at chagford.com> wrote:
> Hi all
>
> Is defaultdict thread safe?
Sometimes. It depends on whether an operation has callbacks to pure
Python.
> Assume I have -
>
> from collections import defaultdict
> my_dict = defaultdict(list)
>
> If two threads call "my_dict['abc'].append(...)" simultaneously, is it
> guaranteed that my_dict['abc'] will end up containing two elements?
Yes.
But, if the constructor is a user defined class, the pure python code
runs for the instantiation and all bets are off.
class A:
def __init__(self):
. . .
my_dict = defaultdict(A) # not thread-safe.
Raymond
More information about the Python-list
mailing list