1
2
3
4
5
6
7
8
9
10
11 import threading
12 import time
13 from functools import wraps
14
15 _POP_DEFAULT=object()
18 "Store elements in a map for the given time"
19
24
25
26 - def clean(self, now = None):
27 "remove old values"
28
29 if now is None:
30 now = time.time()
31 if self.lastClean + self.timeout > now:
32 return
33 for k, (v, t) in self.map.items():
34 if t + self.timeout < now:
35 del self.map[k]
36 self.lastClean = now
37
38
39 - def get(self, key, default):
46
47
55
56
61
63 return key in self.map
64
70
76
80 @wraps(fn)
81 def _closure(self, *args, **kwargs):
82 with self.lock:
83 return fn(self, *args, **kwargs)
84 return _closure
85
87 "Use a simple lock for all read/write access to a map"
88
90 self.map = map
91 self.lock = threading.Lock()
92
93 @Locked_synchronize
95 return key in self.map
96
98 "Deprecated, convert to using 'key in map' form"
99 return key in self
100
101 @Locked_synchronize
107
108 @Locked_synchronize
109 - def get(self, *args):
110 if not args:
111 raise TypeError("get takes at least 1 argument : {0} given".format(len(args)))
112 return self.map.get(*args[:2])
113
114 @Locked_synchronize
117
118 @Locked_synchronize
121
122 @Locked_synchronize
125