Every object I know of in Python can take care of its base class initialization by calling:
super(BaseClass, self).__init__()
This doesn't seem to be the case with a subclass of threading.Thread
, since if I try this in SubClass.__init__()
, I get:
RuntimeError: thread.__init__() not called
What gives this error? I looked at the source for threading.Thread
and it looks like that __init__
method should set Thread.__initialized = True
. I see that all examples use the following __init__
:
class YourThread(threading.Thread):
def __init__(self, *args):
threading.Thread.__init__(self)
# whatev else
But why?
__init__() method is called when an object is initialized. And when you do - Thread. __init__(self) , it just just calling the parent class' __init__() method . Like said in comment you can remove it and the functionality should remain same. In your class the __init__() is completely redundant.
The Thread class represents an activity that is run in a separate thread of control. There are two ways to specify the activity: by passing a callable object to the constructor, or by overriding the run() method in a subclass. No other methods (except for the constructor) should be overridden in a subclass.
Calling the start() function on a terminated thread will result in a RuntimeError indicating that threads can only be started once. Instead, to restart a thread in Python, you must create a new instance of the thread with the same configuration and then call the start() function.
This works fine:
>>> class MyThread(threading.Thread):
... def __init__(self):
... super(MyThread, self).__init__()
I think your code's bug is that you're passing the base class, rather than the current class, to super
-- i.e. you're calling super(threading.Thread, ...
, and that's just wrong. Hard to say since you don't show your failing code, but that's what I infer obliquely from the language you're using!-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With