By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,995 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,995 IT Pros & Developers. It's quick & easy.

Thread-safe dictionary

P: n/a
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!

May 10 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.