'How to properly subclass a subclass of Python Thread?
I have two classes: ContinuousWorker and WorkerA. I would like to pass in an argument that is specific to WorkerA, but also still be able to pass in Thread configuration arguments for use in the the thread.__init__() function. How can I do this?
import threading
class ContinuousWorker(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(args, kwargs) #=> this returns Error: assert group is None, "group argument must be None for now"
# self.Thread.__init__(args, kwargs) #=> this returns Error: 'WorkerA' object has no attribute 'Thread'
class WorkerA(ContinuousWorker):
def __init__(self, argument, *args, **kwargs):
super().__init__(*args, **kwargs)
self.argument = argument
if __name__ == "__main__":
child_class_specific_argument = 1
worker = WorkerA(child_class_specific_argument, thread_name = "threadA", daemon = True)
EDIT: It appears the following is a working solution, can anyone confirm if there is a more pythonic way of doing this?
import threading
class ContinuousWorker(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class WorkerA(ContinuousWorker):
def __init__(self, argument, *args, **kwargs):
super().__init__(*args, **kwargs)
self.argument = argument
if __name__ == "__main__":
child_class_specific_argument = 1
worker = WorkerA(child_class_specific_argument, name = "threadA", daemon = True)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
