From b389cbc3c3f72c3691e5133672d559f13cd39ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Sun, 8 Mar 2020 18:49:22 +0100 Subject: [PATCH] Now with multiprocessing to speed things up --- src/processor.py | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/processor.py b/src/processor.py index cdc8108..089b889 100644 --- a/src/processor.py +++ b/src/processor.py @@ -2,6 +2,7 @@ import argparse import math +import multiprocessing import os from parser import parse_gpx @@ -29,6 +30,24 @@ def create_args_parser(): return parser +def draw_frame(data): + """ + For multiprocessing purposes, this takes a dict + with the options and the data point and creates + a frame + """ + elem = data["datapoint"] + max_speed = data["max"]["speed"] + stills_path = data["stills"] + frame_no = data["frame_no"] + ctx = draw.initialize(1920, 1080) + draw.set_background(ctx, 0, 0, 0, 0.0) + speedometer(ctx, 200, 900, 150, elem[0], max_speed) + draw.save_to_file(ctx, os.path.join(stills_path, f"still-{frame_no:08}.png")) + + return True + + def main(filename, fps, stills_path): data = parse_gpx(filename, fps) max_speed = data.get_maximum('speed') @@ -43,15 +62,25 @@ def main(filename, fps, stills_path): frame = 0 last_frame = data.length() + batch = [] + + print("Preparing batch processing") for elem in data.get_datapoints(): - ctx = draw.initialize(1920, 1080) - draw.set_background(ctx, 0, 0, 0, 0.0) - progress_bar = create_progress_bar(frame, last_frame, 60) - print(f"Current Frame: {frame} {progress_bar} {(frame/last_frame) * 100.0:.2f} %", end="\r") - speedometer(ctx, 200, 900, 150, elem[0], max_speed) - draw.save_to_file(ctx, os.path.join(stills_path, f"still-{frame:08}.png")) + batch.append({"frame_no": frame, + "datapoint": elem, + "stills": stills_path, + "max": {"speed": max_speed}}) frame += 1 + with multiprocessing.Pool() as p: + it = p.imap_unordered(draw_frame, batch) + count = 0 + + for elem in it: + count += 1 + progress_bar = create_progress_bar(count, last_frame, 60) + print(f"Current Frame: {count} {progress_bar} {(count/last_frame) * 100.0:.2f} %", end="\r") + if __name__ == "__main__": parser = create_args_parser()