diff --git a/expiringdict/__init__.py b/expiringdict/__init__.py index ab12eaf..ad27546 100755 --- a/expiringdict/__init__.py +++ b/expiringdict/__init__.py @@ -56,15 +56,16 @@ def __init__(self, max_len, max_age_seconds, items=None): raise ValueError('can not unpack items') def __len__(self): - current_key = iter(self) - for k in current_key: - item = OrderedDict.__getitem__(self, k) - time_added = item[1] - item_age = time.time() - time_added - if item_age > self.max_age: - del self[k] - else: - break + with self.lock: + current_key = iter(self) + for k in current_key: + item = OrderedDict.__getitem__(self, k) + time_added = item[1] + item_age = time.time() - time_added + if item_age > self.max_age: + del self[k] + else: + break return super(ExpiringDict, self).__len__()