container where values expire in python
I am after a thread safe Python cont开发者_如何学编程ainer where the values are automatically removed after a time. Does such a class exist?
Here is a thread safe version of ExpireCounter:
import datetime
import collections
import threading
class ExpireCounter:
"""Tracks how many events were added in the preceding time period
"""
def __init__(self, timeout=1):
self.lock=threading.Lock()
self.timeout = timeout
self.events = collections.deque()
def add(self,item):
"""Add event time
"""
with self.lock:
self.events.append(item)
threading.Timer(self.timeout,self.expire).start()
def __len__(self):
"""Return number of active events
"""
with self.lock:
return len(self.events)
def expire(self):
"""Remove any expired events
"""
with self.lock:
self.events.popleft()
def __str__(self):
with self.lock:
return str(self.events)
which can be used like this:
import time
c = ExpireCounter()
assert(len(c) == 0)
print(c)
# deque([])
c.add(datetime.datetime.now())
time.sleep(0.75)
c.add(datetime.datetime.now())
assert(len(c) == 2)
print(c)
# deque([datetime.datetime(2010, 11, 19, 8, 50, 0, 91426), datetime.datetime(2010, 11, 19, 8, 50, 0, 842715)])
time.sleep(0.75)
assert(len(c) == 1)
print(c)
# deque([datetime.datetime(2010, 11, 19, 8, 50, 0, 842715)])
Perhaps you want an LRU cache. Here's one I've been meaning to try out:
http://pypi.python.org/pypi/repoze.lru
It seems to be thread safe.
This is more or less what I want for now:
from datetime import datetime, timedelta
from collections import deque
class ExpireCounter:
"""Tracks how many events were added in the preceding time period
"""
def __init__(self, timeout=timedelta(seconds=1)):
self.timeout = timeout
self.events = deque()
def add(self):
"""Add event time
"""
self.events.append(datetime.now())
def __len__(self):
"""Return number of active events
"""
self.expire()
return len(self.events)
def expire(self):
"""Remove any expired events
"""
now = datetime.now()
try:
while self.events[0] + self.timeout < now:
self.events.popleft()
except IndexError:
pass # no more events
if __name__ == '__main__':
import time
c = ExpireCounter()
assert(len(c) == 0)
c.inc()
time.sleep(0.75)
c.inc()
assert(len(c) == 2)
time.sleep(0.75)
assert(len(c) == 1)
精彩评论