try to improve memory footprint by rebuilding map dictionary in

intervals
This commit is contained in:
Jakob Ketterl 2019-11-13 18:01:01 +01:00
parent 2a6c7863b1
commit 7489a3bb9d

View File

@ -25,13 +25,23 @@ class Map(object):
def __init__(self): def __init__(self):
self.clients = [] self.clients = []
self.positions = {} self.positions = {}
self.positionsLock = threading.Lock()
def removeLoop(): def removeLoop():
loops = 0
while True: while True:
try: try:
self.removeOldPositions() self.removeOldPositions()
except Exception: except Exception:
logger.exception("error while removing old map positions") logger.exception("error while removing old map positions")
loops += 1
# rebuild the positions dictionary every once in a while, it consumes lots of memory otherwise
if loops == 60:
try:
self.rebuildPositions()
except Exception:
logger.exception("error while rebuilding positions")
loops = 0
time.sleep(60) time.sleep(60)
threading.Thread(target=removeLoop, daemon=True).start() threading.Thread(target=removeLoop, daemon=True).start()
@ -64,7 +74,8 @@ class Map(object):
def updateLocation(self, callsign, loc: Location, mode: str, band: Band = None): def updateLocation(self, callsign, loc: Location, mode: str, band: Band = None):
ts = datetime.now() ts = datetime.now()
self.positions[callsign] = {"location": loc, "updated": ts, "mode": mode, "band": band} with self.positionsLock:
self.positions[callsign] = {"location": loc, "updated": ts, "mode": mode, "band": band}
self.broadcast( self.broadcast(
[ [
{ {
@ -80,13 +91,15 @@ class Map(object):
def touchLocation(self, callsign): def touchLocation(self, callsign):
# not implemented on the client side yet, so do not use! # not implemented on the client side yet, so do not use!
ts = datetime.now() ts = datetime.now()
if callsign in self.positions: with self.positionsLock:
self.positions[callsign]["updated"] = ts if callsign in self.positions:
self.positions[callsign]["updated"] = ts
self.broadcast([{"callsign": callsign, "lastseen": ts.timestamp() * 1000}]) self.broadcast([{"callsign": callsign, "lastseen": ts.timestamp() * 1000}])
def removeLocation(self, callsign): def removeLocation(self, callsign):
self.positions.pop(callsign, None) with self.positionsLock:
# TODO broadcast removal to clients del self.positions[callsign]
# TODO broadcast removal to clients
def removeOldPositions(self): def removeOldPositions(self):
pm = PropertyManager.getSharedInstance() pm = PropertyManager.getSharedInstance()
@ -97,6 +110,11 @@ class Map(object):
for callsign in to_be_removed: for callsign in to_be_removed:
self.removeLocation(callsign) self.removeLocation(callsign)
def rebuildPositions(self):
with self.positionsLock:
p = {key: value for key, value in self.positions.items()}
self.positions = p
class LatLngLocation(Location): class LatLngLocation(Location):
def __init__(self, lat: float, lon: float): def __init__(self, lat: float, lon: float):