首页 > 解决方案 > 如何杀死python中的线程?

问题描述

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

我的线程类(线程。线程):

# 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 中的标签重新启动并停止运行(标签是第二个计数器)

标签: multithreading

解决方案


试试这个:有没有办法杀死一个线程?希望这将是您正在寻找的,希望这就是您正在寻找的


推荐阅读