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
*.pyc

View File

@ -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__':

View File

@ -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()