gpt4 book ai didi

python - 当子类化 threading.Thread 时,我是否必须调用 super.join() ?

转载 作者:太空宇宙 更新时间:2023-11-03 18:07:17 25 4
gpt4 key购买 nike

我有一个 threading.thread 的子类,它正在运行 cherrypy 服务器。我的 join() 方法如下所示:

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

我是否必须明确调用 super.join()?在我显式停止cherrypy服务器之后它到底做了什么?

最佳答案

一般来说,当你重写父类的方法时,你应该调用被重写的方法,除非你完全确定父类是如何工作的,并且这种改变不会破坏它。也就是说,更正式地说,继承代表is-a关系,而你的子类仍然threading.Thread,所以它应该采取相应的行动。事实上,继承联系人的主题非常广泛,如果您感兴趣,您可能想从 Liskov substitution principle 开始。 .

Python 文档介绍了 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.

这意味着,如果您不调用基类 join,您的子类的 join 将不再阻塞调用线程。如果这就是您想要的,您最好使用另一种方法,例如 _exitCherrypyBus 等。

关于python - 当子类化 threading.Thread 时,我是否必须调用 super.join() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26623167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com