Skip to content

Commit

Permalink
Added lock to the length method
Browse files Browse the repository at this point in the history
  • Loading branch information
chanchiem committed Aug 2, 2019
1 parent 444502b commit 4f4b2df
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions expiringdict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()

Expand Down

0 comments on commit 4f4b2df

Please sign in to comment.