bicycle-statistics: Fix memory leak

Signed-off-by: Thomas Klaehn <thomas.klaehn@u-blox.com>
This commit is contained in:
Thomas Klaehn 2019-02-28 09:21:18 +01:00
parent 102642d4ba
commit 06ecfdf0fe
3 changed files with 16 additions and 15 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
data/ *.gpx
.vscode/settings.json .vscode/settings.json
*.pyc *.pyc

View File

@ -1,17 +1,18 @@
import argparse import argparse
import datetime
import logging import logging
import sys import sys
import threading import threading
import time import time
from watchdog.observers import Observer
from gpx2html import Gpx2Html from gpx2html import Gpx2Html
from input_observer import InputObserver
log_level = logging.INFO log_level = logging.INFO
LOG_FILE = "/var/log/bicycle-statistics.log" LOG_FILE = "/var/log/bicycle-statistics.log"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
UPDATE_INTERVAL = 60
logging.basicConfig(format=LOG_FORMAT, level=log_level, filename=LOG_FILE) logging.basicConfig(format=LOG_FORMAT, level=log_level, filename=LOG_FILE)
#logging.basicConfig(format=LOG_FORMAT, level=log_level) #logging.basicConfig(format=LOG_FORMAT, level=log_level)
log = logging.getLogger('bicycle-statistics') log = logging.getLogger('bicycle-statistics')
@ -32,27 +33,24 @@ class myLoop(threading.Thread):
self.run_condition = True self.run_condition = True
self.infolder = infolder self.infolder = infolder
self.outfolder = outfolder self.outfolder = outfolder
self.observer = Observer()
self.in_obs = InputObserver(patterns=["*.gpx"])
def run(self): def run(self):
gpx2html = Gpx2Html(self.infolder, self.outfolder, log) gpx2html = Gpx2Html(self.infolder, self.outfolder, log)
gpx2html.update() gpx2html.update()
self.observer.schedule(self.in_obs, self.infolder) next_update = datetime.datetime.now() + datetime.timedelta(seconds=UPDATE_INTERVAL)
self.observer.start()
while self.run_condition is True: while self.run_condition is True:
new_file = self.in_obs.get_new_file() now = datetime.datetime.now()
log.info("new file triggered: {}".format(new_file)) if now >= next_update:
if new_file:
time.sleep(5) # ensure .gpx file instead of .part file appeared
gpx2html.update() gpx2html.update()
next_update = now + datetime.timedelta(seconds=UPDATE_INTERVAL)
else:
time.sleep(1)
def stop(self): def stop(self):
self.run_condition = False self.run_condition = False
self.in_obs.stop() self.join()
self.observer.stop()
self.observer.join()
def set_log_level(level): def set_log_level(level):
global log_level global log_level
@ -84,7 +82,6 @@ def main():
time.sleep(1) time.sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
my_loop.stop() my_loop.stop()
my_loop.join()
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -45,6 +45,7 @@ def plot_bar_chart(labels, ticklabels, values, title, xlabel, ylabel, filename,
plt.legend() plt.legend()
plt.savefig(filename) plt.savefig(filename)
plt.close('all')
class Gpx2Html(object): class Gpx2Html(object):
def __init__(self, infolder, outfolder, logger): def __init__(self, infolder, outfolder, logger):
@ -52,6 +53,9 @@ class Gpx2Html(object):
self.infolder = infolder self.infolder = infolder
self.outfolder = os.path.abspath(outfolder) self.outfolder = os.path.abspath(outfolder)
if not os.path.exists(self.outfolder):
os.makedirs(self.outfolder)
self.tracks = Tracks(logger) self.tracks = Tracks(logger)
self.update() self.update()