From 06ecfdf0fe186fd4ab095972aee3636b61d7a788 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Thu, 28 Feb 2019 09:21:18 +0100 Subject: [PATCH] bicycle-statistics: Fix memory leak Signed-off-by: Thomas Klaehn --- .gitignore | 2 +- bicycle_statistics/__main__.py | 25 +++++++++++-------------- gpx2html/__init__.py | 4 ++++ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index e68c680..7d28498 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -data/ +*.gpx .vscode/settings.json *.pyc diff --git a/bicycle_statistics/__main__.py b/bicycle_statistics/__main__.py index d6b1bec..54c62c2 100644 --- a/bicycle_statistics/__main__.py +++ b/bicycle_statistics/__main__.py @@ -1,17 +1,18 @@ import argparse +import datetime import logging import sys import threading import time -from watchdog.observers import Observer from gpx2html import Gpx2Html -from input_observer import InputObserver log_level = logging.INFO LOG_FILE = "/var/log/bicycle-statistics.log" 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) log = logging.getLogger('bicycle-statistics') @@ -32,27 +33,24 @@ class myLoop(threading.Thread): self.run_condition = True self.infolder = infolder self.outfolder = outfolder - self.observer = Observer() - self.in_obs = InputObserver(patterns=["*.gpx"]) def run(self): gpx2html = Gpx2Html(self.infolder, self.outfolder, log) gpx2html.update() - self.observer.schedule(self.in_obs, self.infolder) - self.observer.start() + next_update = datetime.datetime.now() + datetime.timedelta(seconds=UPDATE_INTERVAL) + while self.run_condition is True: - new_file = self.in_obs.get_new_file() - log.info("new file triggered: {}".format(new_file)) - if new_file: - time.sleep(5) # ensure .gpx file instead of .part file appeared + now = datetime.datetime.now() + if now >= next_update: gpx2html.update() + next_update = now + datetime.timedelta(seconds=UPDATE_INTERVAL) + else: + time.sleep(1) def stop(self): self.run_condition = False - self.in_obs.stop() - self.observer.stop() - self.observer.join() + self.join() def set_log_level(level): global log_level @@ -84,7 +82,6 @@ def main(): time.sleep(1) except KeyboardInterrupt: my_loop.stop() - my_loop.join() return 0 if __name__ == '__main__': diff --git a/gpx2html/__init__.py b/gpx2html/__init__.py index 0fa2d69..b1283d1 100755 --- a/gpx2html/__init__.py +++ b/gpx2html/__init__.py @@ -45,6 +45,7 @@ def plot_bar_chart(labels, ticklabels, values, title, xlabel, ylabel, filename, plt.legend() plt.savefig(filename) + plt.close('all') class Gpx2Html(object): def __init__(self, infolder, outfolder, logger): @@ -52,6 +53,9 @@ class Gpx2Html(object): self.infolder = infolder self.outfolder = os.path.abspath(outfolder) + if not os.path.exists(self.outfolder): + os.makedirs(self.outfolder) + self.tracks = Tracks(logger) self.update()