Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore old view generation errors #1795

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions hexrdgui/async_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ class AsyncWorker(QRunnable):
'''

def __init__(self, fn, *args, **kwargs):
super(AsyncWorker, self).__init__()
super().__init__()
# Store constructor arguments (re-used for processing)
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = AsyncWorkerSignals()
self.print_error_traceback = True

# If the function signature accepts an 'update_progress'
# function, set it to emit the progress signal.
Expand All @@ -72,7 +73,9 @@ def run(self):
try:
result = self.fn(*self.args, **self.kwargs)
except:
traceback.print_exc()
if self.print_error_traceback:
traceback.print_exc()

exctype, value = sys.exc_info()[:2]
self.signals.error.emit((exctype, value, traceback.format_exc()))
else:
Expand Down
38 changes: 38 additions & 0 deletions hexrdgui/image_canvas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import math
import logging
import sys

from PySide6.QtCore import QThreadPool, QTimer, Signal, Qt
from PySide6.QtWidgets import QFileDialog, QMessageBox
Expand Down Expand Up @@ -72,6 +73,7 @@ def __init__(self, parent=None, image_names=None):
self.blit_manager = BlitManager(self)
self.raw_view_images_dict = {}
self._mask_boundary_artists = []
self._latest_compute_view_worker = None

# Track the current mode so that we can more lazily clear on change.
self.mode = None
Expand Down Expand Up @@ -973,13 +975,21 @@ def show_cartesian(self):

# Run the view generation in a background thread
worker = AsyncWorker(cartesian_viewer)
worker.print_error_traceback = False
worker.signals._image_mode = self.mode
self.thread_pool.start(worker)
self._latest_compute_view_worker = worker

# Get the results and close the progress dialog when finished
worker.signals.result.connect(self.finish_show_cartesian)
worker.signals.error.connect(self.async_worker_error)

def finish_show_cartesian(self, iviewer):
if self.sender() is not self._latest_compute_view_worker.signals:
# A new calculation must have been started while this was running.
# Forget this one...
return

if self.mode != ViewType.cartesian:
# Image mode was switched during generation. Ignore this.
return
Expand Down Expand Up @@ -1040,13 +1050,20 @@ def show_polar(self):

# Run the view generation in a background thread
worker = AsyncWorker(polar_viewer)
worker.print_error_traceback = False
worker.signals._image_mode = self.mode
self.thread_pool.start(worker)
self._latest_compute_view_worker = worker

# Get the results and close the progress dialog when finished
worker.signals.result.connect(self.finish_show_polar)
worker.signals.error.connect(self.async_worker_error)

def finish_show_polar(self, iviewer):
if self.sender() is not self._latest_compute_view_worker.signals:
# A new calculation must have been started while this was running.
# Forget this one...
return

if self.mode != ViewType.polar:
# Image mode was switched during generation. Ignore this.
Expand Down Expand Up @@ -1219,13 +1236,21 @@ def show_stereo(self):

# Run the view generation in a background thread
worker = AsyncWorker(stereo_viewer)
worker.print_error_traceback = False
worker.signals._image_mode = self.mode
self.thread_pool.start(worker)
self._latest_compute_view_worker = worker

# Get the results and close the progress dialog when finished
worker.signals.result.connect(self.finish_show_stereo)
worker.signals.error.connect(self.async_worker_error)

def finish_show_stereo(self, iviewer):
if self.sender() is not self._latest_compute_view_worker.signals:
# A new calculation must have been started while this was running.
# Forget this one...
return

if self.mode != ViewType.stereo:
# Image mode was switched during generation. Ignore this.
return
Expand Down Expand Up @@ -1390,6 +1415,19 @@ def polar_masks_changed(self):
HexrdConfig().polar_masks_reapplied.emit(img)

def async_worker_error(self, error):
if self.sender() is not self._latest_compute_view_worker.signals:
# A new calculation must have been started while this was running.
# This error doesn't matter anymore...
return

if self.sender()._image_mode != self.mode:
# We changed modes while this was running, and it doesn't matter
# anymore...
return

# We disabled traceback printing in the async worker, so print it
# now.
print(error[2], file=sys.stderr)
QMessageBox.critical(self, 'HEXRD', str(error[1]))
msg = f'{str(self.mode)} view error!'
HexrdConfig().emit_update_status_bar(msg)
Expand Down
Loading