forked from douglas-watson/podcasts_export_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.py
84 lines (67 loc) · 2.28 KB
/
worker.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
82
83
84
#!/usr/bin/env python3
#
# Podcasts Export
# ---------------
# Douglas Watson, 2022, MIT License
#
# worker.py: boilerplate to create background tasks that communicate back with
# main thread.
import sys
import traceback
from PySide6.QtCore import QObject, Signal, QRunnable, Slot
class WorkerSignals(QObject):
'''
Defines the signals available from a running worker thread.
Supported signals are:
finished
No data
error
tuple (exctype, value, traceback.format_exc() )
result
object data returned from processing, anything
progress
[0, 100] percentage execution of task, for progress bars.
'''
finished = Signal() # QtCore.Signal
error = Signal(tuple)
result = Signal(object)
progress = Signal(float)
status = Signal(int)
message = Signal(str)
class Worker(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()
@Slot() # QtCore.Slot
def run(self):
'''
Initialise the runner function with passed args, kwargs.
'''
# Retrieve args/kwargs here; and fire processing using them
try:
result = self.fn(
*self.args, **self.kwargs,
set_progress=self.signals.progress.emit,
emit_message=self.signals.message.emit,
)
except:
traceback.print_exc()
exctype, value = sys.exc_info()[:2]
self.signals.error.emit((exctype, value, traceback.format_exc()))
else:
self.signals.result.emit(result) # Return the result of the processing
finally:
self.signals.finished.emit() # Done