Now with multiprocessing to speed things up

This commit is contained in:
José Carlos Cuevas 2020-03-08 18:49:22 +01:00
parent bb10bf0009
commit b389cbc3c3

View file

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