-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.py
More file actions
36 lines (30 loc) · 961 Bytes
/
worker.py
File metadata and controls
36 lines (30 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# worker.py
"""
Mechanism to pull work out of Qt main thread so that the UI doesn't hang
"""
from PySide6.QtCore import QObject, QRunnable, Signal
from sys import exc_info
from traceback import format_exc, print_exc
class WorkerSignals(QObject):
finished = Signal()
error = Signal(tuple)
result = Signal(object)
class Worker(QRunnable):
def __init__(self, fn, *args, **kwargs):
super().__init__()
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()
def run(self):
# Run the passed fn with the passed args
try:
result = self.fn(*self.args, **self.kwargs)
except BaseException:
print_exc()
exctype, value = exc_info()[:2]
self.signals.error.emit((exctype, value, format_exc()))
else:
self.signals.result.emit(result)
finally:
self.signals.finished.emit()