471,086 Members | 1,197 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 software developers and data experts.

Thread safe object cache without locking

I'm trying to make a thread safe object cache without locking.

The objects are cached by the id of the data dict given in __new__.
Objects are removed from the cache as soon as they are no longer
referenced. The type of the data must be a Python dict (comes from an
external source).

Here's what I've got so far:

import time
from weakref import ref

cache = {} # {id(data): ref_to_Foo(data), ...}

class Foo(object):

__slots__ = ('__weakref__', '_data')

def __new__(cls, data):
new_self = object.__new__(cls)

def remove(wr, _cache=cache, _data=data):
del _cache[id(_data)]
except KeyError:
pass # should never happen

while 1:
self = cache.setdefault(id(data), ref(new_self, remove))()
self._data = data
return self # this is new_self or a cached instance
except AttributeError:
# happens rarely: got a 'dead' cached instance
time.sleep(0) # small delay before retrying

# Usage example:
data = {'a':1, 'b':2}
a = Foo(data)
b = Foo(data)
assert a is b

I've got couple of assumptions based on I think the code is working

a) Python's dict is thread safe, i.e. setdefault can't replace an
existing value until the key has been removed - no matter how many
threads are calling it simultaneously. Additionally, only one thread
succeeds in setting the new value.

b) Unreferenced ref(new_self, remove) objects are garbage collected
without the callbacks being called.

c) The remove() keeps a reference to the data dict. That should prevent
Python from reusing the object id too soon.

Are these assumptions correct? Do you have ideas for improvements
(especially regarding speed)? I'm restricted to Py2.3+ compatibility.


Sep 15 '05 #1
0 1325

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Roman Suzi | last post: by
3 posts views Thread by Philip V Pham | last post: by
5 posts views Thread by nicolas_riesch | last post: by
1 post views Thread by William Sullivan | last post: by
7 posts views Thread by reyesflaco | last post: by
12 posts views Thread by Peter K | last post: by
5 posts views Thread by Nuno Magalhaes | last post: by
29 posts views Thread by NvrBst | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.