Thread-safe dictionary

"Martin v. Löwis" martin at v.loewis.de
Sat May 12 05:40:01 EDT 2007


> - 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



More information about the Python-list mailing list