Thread-safe dictionary

T

tuom.larsen

Hi,

please consider the following code:


from __future__ import with_statement

class safe_dict(dict):
def __init__(self, *args, **kw):
self.lock = threading.Lock()
dict.__init__(self, *args, **kw)
def __getitem__(self, key):
with self.lock:
return dict.__getitem__(self, key)
def __setitem__(self, key, value):
with self.lock:
dict.__setitem__(self, key, value)
def __delitem__(self, key):
with self.lock:
dict.__delitem__(self, key)


- would I need to override another methods e.g. update() or items() in
order to remain thread-safe or is this enough?
- in __getitem__, does it release the lock after returning the item?
- wouldn't it be better to use threading.RLock, mutex, ... instead?

Thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top