Thread-safe dictionary

tuom.larsen at gmail.com tuom.larsen at gmail.com
Thu May 10 14:25:12 EDT 2007


On May 10, 3:57 pm, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> 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.

thanks, sounds good!

but that again:



from __future__ import with_statement

class safe_dict(dict):
    def __init__(self):
        self.lock = threading.Lock()
        self.d = {}
    def __getitem__(self, key):
        with self.lock:
            return self.d[key]
    def __setitem__(self, key, value):
        with self.lock:
            self.d[key] = value
    def __delitem__(self, key):
        with self.lock:
            del self.d[key]



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




More information about the Python-list mailing list