Search code examples
pythonmultithreadingpython-2.7cherrypy

Do I have to call super.join() when subclassing threading.Thread?


I have a subclass of threading.thread that is running a cherrypy server. My join() method looks like this:

def join(self):
    cherrypy.engine.exit()
    super(MyThread, self).join()

Do I have to explicitly have to call super.join()? What exactly does it do after I have explicitly stopped the cherrypy server?


Solution

  • In general, when you override a method of parent class, you should call the overridden method, unless you are absolutely sure how parent class works, and this change won't break it. That is to say more formally, inheritance represents is-a relationship, and your subclass still is a threading.Thread, so it should act accordingly. The topic of an inheritance contact is, in fact, quite broad, and if you're interested you may may want to start with Liskov substitution principle.

    Python documentation states about threading.Thread.join([timeout]).

    Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.

    This means that if you don't call the base join, your subclass' join will no longer block the calling thread. If that is what you want, you're better just make another method like _exitCherrypyBus or so.