From 4f4b2df8db604a1651d2f5ce0ff2c0898b91ff94 Mon Sep 17 00:00:00 2001 From: chanchiem Date: Thu, 1 Aug 2019 21:18:42 -0700 Subject: [PATCH] Added lock to the length method --- expiringdict/__init__.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) 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__()