Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad idea to start a thread in init method? [closed]

Tags:

python

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()
like image 356
rdp Avatar asked Sep 19 '25 20:09

rdp


1 Answers

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.

like image 161
rlee827 Avatar answered Sep 21 '25 13:09

rlee827