I would like to implement an entire dictionary subclass that is thread safe in every way - all the get and set methods. Currently, I am simply providing access to it via a helper function like this:
# Don't access directly (I am not a big fan of name mangling)
mydict = dict()
lock = threading.Rlock()
def getdict():
"""Safe way to access the dict."""
with lock:
return mydict
I think it might be better if I do it by subclassing the dict, since it would avoid awkward calls like getdict()["key"] = "value"
. The wrapper classes from collections
library looks promising, but I am not sure which methods needs to be overridden in order to make it entirely safe. __getattr__
and __setattr__
would be two, but what about update
? Are there any more? Is this even a good idea?