-
Notifications
You must be signed in to change notification settings - Fork 5
/
register.py
46 lines (40 loc) · 1.49 KB
/
register.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#
# register.py - This module provides a class to register and lookup objects by name.
# It also keeps track of the time each object was registered.
#
from threading import Lock
import time
from logging import getLogger
log = getLogger(__name__)
class Registry(object):
def __init__(self):
self._lock = Lock()
self.map_registry = dict()
self.timestamp = dict()
def register(self, map_name, map_object):
with self._lock:
self.map_registry[map_name] = map_object
self.timestamp[map_name] = time.time()
def lookup(self, map_name):
with self._lock:
try:
return self.map_registry[map_name]
except Exception as exc:
log.error(f"Exception looking up {map_name} map: {exc}")
return None
def get_timestamp(self, map_name):
with self._lock:
try:
return self.timestamp[map_name]
except Exception as exc:
log.error(f"Exception looking up timestamp for {map_name} map: {exc}")
return None
def get_age(self, map_name):
with self._lock:
if map_name not in self.timestamp:
return -1 # no age, as it was never registered
try:
return time.time() - self.timestamp[map_name]
except Exception as exc:
log.error(f"Exception looking up age for {map_name} map: {exc}")
return None