Skip to content

Commit

Permalink
enh: prevent GUI from locking when transferring large file
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Nov 30, 2023
1 parent e4b23f7 commit 191ea58
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
0.6.0
- feat: generalize GUI to use all recipes
- enh: prevent GUI from locking when transferring large file
- enh: re-use tree count information for transfer progress bar
- enh: display the actual directory tree instead of a table
- enh: compute file hash while copying, avoiding reading data twice
Expand Down
33 changes: 27 additions & 6 deletions mpl_data_cast/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import signal
import pathlib
import sys
import threading
import traceback
from typing import Dict

import dclab
import h5py
Expand Down Expand Up @@ -151,10 +153,20 @@ def on_task_transfer(self) -> None:
self.widget_output.path)

tree_counter = self.widget_input.tree_counter
with Callback(self, tree_counter) as path_callback:
with CastingCallback(self, tree_counter) as callback:
# run the casting operation in a separate thread
caster = CastingThread(rp, callback=callback)
caster.start()

result = rp.cast(path_callback=path_callback)
self.widget_output.trigger_recount_objects()
while not caster.result:
QtWidgets.QApplication.processEvents(
QtCore.QEventLoop.ProcessEventsFlag.AllEvents, 300)
time.sleep(.1)

caster.join()
result = caster.result

self.widget_output.trigger_recount_objects()

if result["success"]:
self.progressBar.setValue(100)
Expand All @@ -177,7 +189,7 @@ def on_task_transfer(self) -> None:
self.pushButton_transfer.setEnabled(True)


class Callback:
class CastingCallback:
"""Makes it possible to execute code everytime a file was processed.
Used for updating the progress bar and calculating the processing rate."""

Expand Down Expand Up @@ -208,11 +220,20 @@ def __call__(self, path) -> None:
# go to undetermined state
self.gui.progressBar.setRange(0, 0)

QtWidgets.QApplication.processEvents(
QtCore.QEventLoop.ProcessEventsFlag.AllEvents, 300)
self.counter += 1


class CastingThread(threading.Thread):
def __init__(self, rp, callback, *args, **kwargs):
super(CastingThread, self).__init__(*args, **kwargs)
self.rp = rp
self.callback = callback
self.result = {}

def run(self):
self.result = self.rp.cast(callback=self.callback)


def excepthook(etype, value, trace) -> None:
"""
Handler for all unhandled exceptions.
Expand Down

0 comments on commit 191ea58

Please sign in to comment.