I have a class which acts like a Queue in which I am creating a worker thread. This worker thread would be responsible for processing each item from the queue.
Since an instance of this class should always have a worker thread, I am creating and starting the worker thread in the init method itself.
I would like to know if it is a bad practice to do so.
e.g
class CustomQueue:
def __init__(self):
# some initialization
self.worker = threading.Thread(target=sometarget)
self.worker.start()
It is OK, but you will need to add a __del__
method that stops the thread so that the thread doesn't hang around after the CustomQueue object dissapears. This __del__
method will be called when the CustomQueue instance is garbage collected.
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