feat(builder): update multithreading

This commit is contained in:
Haoyu Xu
2021-08-08 21:04:00 -04:00
parent 10778e139a
commit 431da02fc2
3 changed files with 134 additions and 140 deletions

View File

@@ -22,6 +22,7 @@ class AtlasReader:
self.images.append(line) self.images.append(line)
line = f.readline() line = f.readline()
self.copy() self.copy()
return self.images
def copy(self): def copy(self):
shutil.copyfile( shutil.copyfile(

View File

@@ -26,21 +26,28 @@ class Builder:
return return
def build(self, operator_name): def build(self, operator_name):
self.content_processor = ContentProcessor(self.config, operator_name) thread_map = [
assets_thread = threading.Thread( dict(
target=self.__build_assets, target=self.__build_assets,
args=(operator_name,), args=(operator_name,),
daemon=True, daemon=True,
) ),
settings_thread = threading.Thread( dict(
target=self.__build_settings, target=self.__build_settings,
args=(operator_name,), args=(operator_name,),
daemon=True, daemon=True,
) )
assets_thread.start() ]
settings_thread.start() threads = list()
assets_thread.join() self.content_processor = ContentProcessor(self.config, operator_name)
settings_thread.join()
for item in thread_map:
thread = threading.Thread(**item)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
# use content processor to generate operator settings # use content processor to generate operator settings
def __build_settings(self, operator_name): def __build_settings(self, operator_name):
@@ -56,62 +63,88 @@ class Builder:
return return
def __build_assets(self, operator_name): def __build_assets(self, operator_name):
use_skel = self.config["operator"]["use_skel"] use_skel = self.config["operator"]["use_skel"]
source_path = self.config["operator"]["source_folder"].format(name=operator_name)
target_path = self.config["operator"]["target_folder"].format(name=operator_name)
common_name = self.config["operators"][operator_name]["_operator_settings.js"]["filename"]
fallback_name = self.config["operators"][operator_name]["index.html"]["fallback_name"]
id_name = self.config["operators"][operator_name]["index.html"]["id"]
file_paths = dict( file_paths = dict(
json=target_path + common_name + ".json", source=self.config["operator"]["source_folder"].format(name=operator_name),
atlas=target_path + common_name + ".atlas", target=self.config["operator"]["target_folder"].format(name=operator_name),
skel=target_path + common_name + ".skel", common_name=self.config["operators"][operator_name]["_operator_settings.js"]["filename"],
fallback_name=self.config["operators"][operator_name]["index.html"]["fallback_name"],
id_name=self.config["operators"][operator_name]["index.html"]["id"]
) )
operator_file = pathlib.Path.cwd().joinpath(target_path, "..", "{}_assets.js".format(id_name)) operator_file = pathlib.Path.cwd().joinpath(file_paths["target"], "..", "{}_assets.js".format(file_paths["id_name"]))
if operator_file.exists() is False or self.rebuild is True: if operator_file.exists() is False or self.rebuild is True:
print("Building operator data for {}...".format(operator_name)) print("Building operator data for {}...".format(operator_name))
alpha_composite_threads = list()
png_to_base64_threads = list()
prefix = "window.operatorAssets = " prefix = "window.operatorAssets = "
data = dict() data = dict()
ar = AtlasReader(source_path + common_name, target_path + common_name) thread_map = [
dict(
target=self.__ar_thread,
args=(
file_paths,
data
),
daemon=True,
),
dict(
target=self.__skeleton_binary_thread,
args=(
file_paths,
data,
use_skel
),
daemon=True,
),
dict(
target=self.__fallback_thread,
args=(
file_paths,
data
),
daemon=True,
),
]
threads = list()
for item in thread_map:
thread = threading.Thread(**item)
threads.append(thread)
thread.start()
skeleton_binary_thread = threading.Thread( for thread in threads:
target=SkeletonBinary, thread.join()
args=(source_path + common_name, target_path + common_name, use_skel),
daemon=True, jsonContent = prefix + str(data)
) with open(operator_file, "w") as f:
ar_thread = threading.Thread( f.write(jsonContent)
target=ar.get_images,
daemon=True, print("Finished building operator data for {}.".format(operator_name))
) else:
atlas_base64_thread = threading.Thread( print("Operator data for {} has been built.".format(operator_name))
return
def __ar_thread(self, file_paths, data):
source_path = file_paths["source"]
target_path = file_paths["target"]
common_name = file_paths["common_name"]
png_to_base64_threads = list()
alpha_composite_threads = list()
ar = AtlasReader(source_path + common_name, target_path + common_name)
images = ar.get_images()
atlas_to_base64_thread = threading.Thread(
target=self.__atlas_to_base64, target=self.__atlas_to_base64,
args=( args=(
file_paths["atlas"], target_path + common_name + ".atlas",
data, data,
self.config["server"]["operator_folder"] + common_name + ".atlas", self.config["server"]["operator_folder"] + common_name + ".atlas",
), ),
daemon=True, daemon=True,
) )
fallback_thread = threading.Thread( atlas_to_base64_thread.start()
target=AlphaComposite,
args=(source_path + fallback_name, target_path + "../{}".format(fallback_name)),
daemon=True,
)
ar_thread.start() for item in images:
skeleton_binary_thread.start()
fallback_thread.start()
ar_thread.join()
atlas_base64_thread.start()
# alpha composite for live 2d assets
for item in ar.images:
alpha_composite_thread = threading.Thread( alpha_composite_thread = threading.Thread(
target=AlphaComposite, target=AlphaComposite,
args=(source_path + item, target_path + item), args=(source_path + item, target_path + item),
@@ -119,11 +152,10 @@ class Builder:
) )
alpha_composite_threads.append(alpha_composite_thread) alpha_composite_threads.append(alpha_composite_thread)
alpha_composite_thread.start() alpha_composite_thread.start()
for thread in alpha_composite_threads: for thread in alpha_composite_threads:
thread.join() thread.join()
for item in ar.images: for item in images:
png_to_base64_thread = threading.Thread( png_to_base64_thread = threading.Thread(
target=self.__png_to_base64, target=self.__png_to_base64,
args=( args=(
@@ -136,48 +168,35 @@ class Builder:
png_to_base64_threads.append(png_to_base64_thread) png_to_base64_threads.append(png_to_base64_thread)
png_to_base64_thread.start() png_to_base64_thread.start()
skeleton_binary_thread.join()
if use_skel is True:
skel_base64_thread =threading.Thread(
target=self.__skel_to_base64,
args=(
file_paths["skel"],
data,
self.config["server"]["operator_folder"] + common_name + ".skel",
),
daemon=True,
)
skel_base64_thread.start()
skel_base64_thread.join()
else:
json_base64_thread =threading.Thread(
target=self.__json_to_base64,
args=(
file_paths["json"],
data,
self.config["server"]["operator_folder"] + common_name + ".json",
),
daemon=True,
)
json_base64_thread.start()
json_base64_thread.join()
# join remaining threads
for thread in png_to_base64_threads: for thread in png_to_base64_threads:
thread.join() thread.join()
atlas_to_base64_thread.join()
atlas_base64_thread.join() def __skeleton_binary_thread(self, file_paths, data, use_skel):
fallback_thread.join() source_path = file_paths["source"]
target_path = file_paths["target"]
common_name = file_paths["common_name"]
jsonContent = prefix + str(data) SkeletonBinary(source_path + common_name, target_path + common_name, use_skel)
with open(operator_file, "w") as f: if use_skel is True:
f.write(jsonContent) self.__skel_to_base64(
target_path + common_name + ".skel",
print("Finished building operator data for {}.".format(operator_name)) data,
self.config["server"]["operator_folder"] + common_name + ".skel",
)
else: else:
print("Operator data for {} has been built.".format(operator_name)) self.__json_to_base64(
return target_path + common_name + ".json",
data,
self.config["server"]["operator_folder"] + common_name + ".json",
)
def __fallback_thread(self, file_paths, data):
source_path = file_paths["source"]
target_path = file_paths["target"]
fallback_name = file_paths["fallback_name"]
AlphaComposite(source_path + fallback_name, target_path + "../{}".format(fallback_name))
def __json_to_base64(self, path, dict=None, key=None): def __json_to_base64(self, path, dict=None, key=None):
with open(pathlib.Path.cwd().joinpath(path), "r") as f: with open(pathlib.Path.cwd().joinpath(path), "r") as f:

View File

@@ -1,26 +0,0 @@
import pathlib
class HtmlProcessor:
def __init__(self, config):
self.config = config
pass
def process(self, operator, file_path):
with open(pathlib.Path.cwd().joinpath(file_path), "r") as f:
content = f.read()
return content.format(
title=self.config["operators"][operator]["title"],
version=self.__get_version(),
operator_logo=self.config["operators"][operator]["logo_name"]
)
def build(self, operator, source_path, target_path):
content = self.process(operator, source_path)
with open(pathlib.Path.cwd().joinpath(target_path), "w") as f:
f.write(content)
def __get_version(self):
with open(pathlib.Path.cwd().joinpath("Version"), "r") as f:
version = f.read()
return version