Python 线程停止执行代码

Python threading stopping code from being executed

我这里有这段代码:

from threading import Thread
import time

class TestClass():

    def __init__ (self, name):
        self.name = name

        self.thread = Thread(target=self.run())
        
        self.thread.start()

    def run(self):
        while True:
            print(self.name)

            time.sleep(1)

test = TestClass('word')

print('done')

所以基本上这只会一直打印 'word',而不会打印 'done'。这是我遇到的问题的一个小演示,导致线程卡住并且它正在阻止其他代码行的执行。您可以自己尝试这个,您会得到相同的结果。我在这里遗漏了什么吗?

这应该可行,并且是使用线程的推荐方式 class。

from threading import Thread
import time

class TestClass(Thread):

    def __init__ (self, name):
        super().__init__()
        self.name = name
        self.start()

    def run(self):
        while True:
            print(self.name)

            time.sleep(1)

test = TestClass('word')

print('done')