1
2
3
4
5
6
7
8
9
10
11
12
13
14 import threading
15 import time
16 from functools import wraps
17
18
19 -class Timed(object):
20 "Store elements in a map for the given time"
21
26
27
28 - def clean(self, now = None):
29 "remove old values"
30
31 if now is None:
32 now = time.time()
33 if self.lastClean + self.timeout > now:
34 return
35 for k, (v, t) in self.map.items():
36 if t + self.timeout < now:
37 del self.map[k]
38 self.lastClean = now
39
40
41 - def get(self, key, default):
48
49
57
58
63
64
70
74 @wraps(fn)
75 def _closure(self, *args, **kwargs):
76 with self.lock:
77 return fn(self, *args, **kwargs)
78 return _closure
79
81 "Use a simple lock for all read/write access to a map"
82
84 self.map = map
85 self.lock = threading.Lock()
86
87 @Locked_synchronize
89 return key in self.map
90
92 "Deprecated, convert to using 'key in map' form"
93 return key in self
94
95 @Locked_synchronize
96 - def get(self, *args):
97 if not args:
98 raise TypeError("get takes at least 1 argument : {0} given".format(len(args)))
99 return self.map.get(*args[:2])
100
101 @Locked_synchronize
104
105 @Locked_synchronize
108
109 @Locked_synchronize
112