From 8555336238d99a48efb22863ce79be1b2b60be10 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Fri, 26 Apr 2024 17:37:49 -0400 Subject: [PATCH 1/2] unzip_parts.py - Reduce working memory size by reading and writing smaller chunks into memory vs. the whole zip file parts Reduces the data read into ram from 76MB (the size of each zip file at present) to 1MB. --- unzip_parts.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/unzip_parts.py b/unzip_parts.py index 99d1e2e..9d00688 100644 --- a/unzip_parts.py +++ b/unzip_parts.py @@ -25,10 +25,9 @@ def unzip_parts(path): # Open the split file with open(split_path, "rb") as split_file: # Read the file data - file_data = split_file.read() - - # Append the file data to the original file - db.write(file_data) + while (file_data := split_file.read(1024 * 1024)): + # Append the file data to the original file + db.write(file_data) # Delete the split file os.unlink(split_path) From cf2de774e72dee2c129a8ce9b3f21140f4d56343 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Sat, 27 Apr 2024 16:32:29 -0400 Subject: [PATCH 2/2] LogBoxHandler - Marshal widget interactions to the main thread for proper thread safety --- events.py | 1 + mainwindow.py | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/events.py b/events.py index 504cce8..fcb5bb4 100644 --- a/events.py +++ b/events.py @@ -8,3 +8,4 @@ AssignPartsEvent, EVT_ASSIGN_PARTS_EVENT = NewEvent() PopulateFootprintListEvent, EVT_POPULATE_FOOTPRINT_LIST_EVENT = NewEvent() UpdateSetting, EVT_UPDATE_SETTING = NewEvent() +LogboxAppendEvent, EVT_LOGBOX_APPEND_EVENT = NewEvent() diff --git a/mainwindow.py b/mainwindow.py index 9cfa916..7b5cc30 100644 --- a/mainwindow.py +++ b/mainwindow.py @@ -15,11 +15,13 @@ from .const import Column from .events import ( EVT_ASSIGN_PARTS_EVENT, + EVT_LOGBOX_APPEND_EVENT, EVT_MESSAGE_EVENT, EVT_POPULATE_FOOTPRINT_LIST_EVENT, EVT_RESET_GAUGE_EVENT, EVT_UPDATE_GAUGE_EVENT, EVT_UPDATE_SETTING, + LogboxAppendEvent, ) from .fabrication import Fabrication from .helpers import ( @@ -501,6 +503,7 @@ def __init__(self, parent, kicad_provider=KicadProvider()): self.Bind(EVT_ASSIGN_PARTS_EVENT, self.assign_parts) self.Bind(EVT_POPULATE_FOOTPRINT_LIST_EVENT, self.populate_footprint_list) self.Bind(EVT_UPDATE_SETTING, self.update_settings) + self.Bind(EVT_LOGBOX_APPEND_EVENT, self.logbox_append) self.enable_part_specific_toolbar_buttons(False) @@ -897,6 +900,10 @@ def update_settings(self, e): self.settings[e.section][e.setting] = e.value self.save_settings() + def logbox_append(self, e): + """Write text to the logbox.""" + self.logbox.WriteText(e.msg) + def load_settings(self): """Load settings from settings.json.""" with open(os.path.join(PLUGIN_PATH, "settings.json"), encoding="utf-8") as j: @@ -1092,7 +1099,7 @@ def init_logger(self): handler1 = logging.StreamHandler(sys.stderr) handler1.setLevel(logging.DEBUG) # and to our GUI - handler2 = LogBoxHandler(self.logbox) + handler2 = LogBoxHandler(self.logbox, self) handler2.setLevel(logging.DEBUG) formatter = logging.Formatter( "%(asctime)s - %(levelname)s - %(funcName)s - %(message)s", @@ -1112,15 +1119,15 @@ def __del__(self): class LogBoxHandler(logging.StreamHandler): """Logging class for the logging textbox at th ebottom of the mainwindow.""" - def __init__(self, textctrl): + def __init__(self, textctrl, event_destination): logging.StreamHandler.__init__(self) self.textctrl = textctrl + self.event_destination = event_destination def emit(self, record): - """Pokemon exception that hopefully helps getting this working with threads.""" - try: - msg = self.format(record) - self.textctrl.WriteText(msg + "\n") - self.flush() - except: # pylint: disable=bare-except - pass + """Marshal the event over to the main thread.""" + msg = self.format(record) + wx.QueueEvent(self.event_destination, LogboxAppendEvent( + msg=f"{msg}\n" + ) + )