-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheat_transfer_workers.py
81 lines (63 loc) · 2.35 KB
/
heat_transfer_workers.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
This module is responsible for defining Workers that enable multihreading.
"""
import traceback
import sys
from PyQt5 import QtCore # type: ignore
class WorkerSignals(QtCore.QObject):
'''
Defines the signals available from a running worker thread.
Supported signals are:
finished
Just a signal, not carrying any data.
error
`tuple` (exctype, value, traceback.format_exc() )
result
`object` data returned from processing, can be any object
progress
`int` - indicating whether the simulation is running (1) or not (0)
'''
finished = QtCore.pyqtSignal()
error = QtCore.pyqtSignal(tuple)
result = QtCore.pyqtSignal(object)
progress = QtCore.pyqtSignal(int)
class Worker(QtCore.QRunnable):
'''
Worker thread
Inherits from QRunnable to handler worker thread setup, signals and wrap-up.
:param callback: The function callback to run on this worker thread.
Supplied args and kwargs will be passed through to the runner.
:type callback: function
:param args: Arguments to pass to the callback function
:param kwargs: Keywords to pass to the callback function
'''
def __init__(self, fn, *args, **kwargs):
super(Worker, self).__init__()
# Store constructor arguments (re-used for processing)
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()
# Add the callback to our kwargs, so we are notified of the progress
self.kwargs['progress_callback'] = self.signals.progress
@QtCore.pyqtSlot()
def run(self):
'''
Initialise the runner function with passed args, kwargs.
Running the function.
'''
# Retrieve args/kwargs here; and fire processing using them
try:
result = self.fn(*self.args, **self.kwargs)
# In case of any exception transfer them as signals
except Exception as e:
print(e)
traceback.print_exc()
exctype, value = sys.exc_info()[:2]
self.signals.error.emit((exctype, value, traceback.format_exc()))
# If we did not encounter any exception, return the result
else:
self.signals.result.emit(result)
# In any case return the finished signal
finally:
self.signals.finished.emit()