Thread-safe dictionary

tuom.larsen at gmail.com tuom.larsen at gmail.com
Sat May 12 21:00:21 EDT 2007


On May 12, 11:40 am, "Martin v. Löwis" <mar... at v.loewis.de> wrote:
> > - in __getitem__, does it release the lock after returning the item?
>
> Yes, it does.
>
> > - wouldn't it be better to use threading.RLock, mutex, ... instead?
>
> Better in what sense? Performance-wise? Semantically? Performance-wise,
> the best thing would be to do
>
> safe_dict = dict
>
> because the builtin dict is already thread-safe unless user-defined
> __hash__ or __eq__ methods get invoked (in which case the dictionary
> still works correctly - it's only that it may get modified while
> __hash__ or __eq__ is running).
>
> Semantically, it would be better to use a threading.RLock, if you
> expect that __hash__ or __eq__ may access the dictionary recursively.
>
> Regards,
> Martin

Thank you!




More information about the Python-list mailing list