Skip to content

Commit

Permalink
added error signal to thread
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonzorn committed Jul 16, 2024
1 parent 09a652b commit 998ec2c
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions nlightreader/utils/threads.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import traceback
from typing import Callable

from PySide6.QtCore import (
Expand All @@ -11,6 +12,7 @@


class Signals(QObject):
error = Signal(Exception)
finished = Signal(object)


Expand All @@ -22,6 +24,7 @@ def __init__(
kwargs=None,
*,
callback=None,
error_callback=None,
):
super().__init__()
if kwargs is None:
Expand All @@ -32,11 +35,18 @@ def __init__(
self.signals = Signals()
if callback:
self.signals.finished.connect(callback)
if error_callback:
self.signals.error.connect(error_callback)

@Slot()
def run(self):
result = self._target(*self._args, **self._kwargs)
self.signals.finished.emit(result)
try:
result = self._target(*self._args, **self._kwargs)
except Exception as e:
traceback.print_exc()
self.signals.error.emit(e)
else:
self.signals.finished.emit(result)


class Worker(NlThread, QRunnable):
Expand Down Expand Up @@ -66,8 +76,15 @@ def __init__(
kwargs=None,
*,
callback=None,
error_callback=None,
):
super().__init__(target, args, kwargs, callback=callback)
super().__init__(
target,
args,
kwargs,
callback=callback,
error_callback=error_callback,
)

def start(self, pool=None):
if pool is None:
Expand Down Expand Up @@ -104,5 +121,12 @@ def __init__(
kwargs=None,
*,
callback=None,
error_callback=None,
):
super().__init__(target, args, kwargs, callback=callback)
super().__init__(
target,
args,
kwargs,
callback=callback,
error_callback=error_callback,
)

0 comments on commit 998ec2c

Please sign in to comment.