Re: Thread-safe dictionary



Jean-Paul Calderone <exarkun@xxxxxxxxxx> wrote:

- would I need to override another methods e.g. update() or items() in
order to remain thread-safe or is this enough?

No, you'll need to protect almost everything. items may be safe. update,
clear, get, has_key, pop, and setdefault all need a lock in the general
case.

Also be aware that some Python internals bypass things like __getitem__ in
dict subclasses, so the lock will be ignored if (for example) you use the
dict as a namespace for exec.

- in __getitem__, does it release the lock after returning the item?
- wouldn't it be better to use threading.RLock, mutex, ... instead?


The builtin dict type is already thread safe.

It very much depends on what you mean by 'thread safe'. Calls to dict
methods from multiple threads won't result in Python getting into an
inconsistent state and crashing, but they aren't necessarily atomic either.

In particular, any method which can modify the content of the dictionary
could release an instance of a class with a __del__ method written in
Python and other threads will be able to run at that point.
Likewise any lookup in a dict where a key is an instance of a class with
user-defined comparison method could allow Python code and therefore other
threads to run.

Of course if your particular dict objects all use simple types this may not
matter, but it needs a careful programmer to use an ordinary Python dict
(or list) safely across threads.

IMHO you are probably best to write a thread-safe class which uses an
ordinary dict (and has a nicely limited interface) rather than trying to
produce a completely thread-safe dict type.
.



Relevant Pages

  • Re: dict critique
    ... safe way to do it. ... making the [dict update] variable a facade for accessing the ... hackery and suitable variable traces. ...
    (comp.lang.tcl)
  • Can you introduce some book about python?
    ... appending key-value pairs to a dict (Peter ... Is Python suitable for a huge, ... >> to the list on each loop... ...
    (comp.lang.python)
  • Re: Thoughts about Python
    ... > learning Python. ... Since several people have already mentioned that the syntax ... there is very little in any programming language that is ... filed in the dict under the old rather than the new hash code. ...
    (comp.lang.python)
  • Re: Overloading operators on the rigth.
    ... which mostly holds 'slots' which are pointers to C functions. ... Typically no Python names are involved during phase 1 (the ... fully support any mapping in lieu of the class dict, ... tinkering with internals that are currently coded in C. ...
    (comp.lang.python)
  • automatic static types (metaclass), attribute-order extraction = python instead of XML-likes - and s
    ... order of the fields to follow the order they are written in the python ... iterable of key,value tuples instead of plain dict. ... (and not the attribute namespaces, ... from statictype2 import StaticTyper, StaticType ...
    (comp.lang.python)