如何在python中杀死线程?

时间:2019-04-10 13:39:57

标签: multithreading

我有这个课程,这是我的话题。通过创建该类的实例,我创建了我的线程,因此通过调用其stop方法,它应该被终止或终止,但不是。不知道为什么如果有人可以帮助我,我将不胜感激。

Class MyThread(threading.Thread):

# Thread class with a _stop() method.

# The thread itself has to check

# regularly for the stopped() condition.

def __init__(self, last_time=0, start_time=0, label="", root="",  speed_mode=""):

    super(MyThread, self).__init__()
    self.label = label
    self.last_time = last_time
    self.start_time = start_time
    self.root = root
    self.speed_mode = speed_mode
    self._stop_event = threading.Event()

    # function using _stop function

def start_counter(self):
    self.start_time += 1
    self.label['text'] = self.start_time
    # self.start_time = start_time
    if self.start_time < self.last_time:
        self.label.after(DIC_TIME[self.speed_mode], self.start_counter)

def stop(self):

    self._stop_event.set()

def stopped(self):

    return self._stop_event.isSet()

def run(self):
    self.start_counter()
    while True:
        if self.stopped():
            return

我希望通过调用MyThread类的stop方法,使tinter canvas中的标签重新启动并停止运行(label是第二个计数器)

1 个答案:

答案 0 :(得分:0)

尝试一下:Is there any way to kill a Thread?希望它是您要寻找的东西,希望它是您要寻找的东西