-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_thread.py
32 lines (26 loc) · 891 Bytes
/
custom_thread.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
from threading import Thread
class CustomThread(Thread):
"""
Custom thread class that permits to get the return value of a function.
Based on https://superfastpython.com/thread-return-values/#Need_to_Return_Values_From_a_Thread
"""
def __init__(self, target: callable, args: list) -> None:
"""
Constructor
Parameters
------------
func: function to be executed in a new thread
params: parameters of the function
"""
# execute the base constructor
Thread.__init__(self)
# set a default value
self.value = None
self.func = target
self.params = args
def run(self) -> None:
"""
Run the thread, saving the return value in an instance variable
"""
# store data in an instance variable
self.value = self.func(**self.params)