首页 > 解决方案 > 如何停止/跳出线程无限的while循环?

问题描述

我需要通过 stop_button 函数停止 while 循环。尝试了几种方法,但都失败了。求指点,谢谢。

def start_button():
    t1 = threading.Thread(target=thread1)
    t1.start()

def thread1():
    while True:
        start()

def stop_button():
    pass

标签: pythonpython-multithreading

解决方案


也许这

flag = False

def start_button():
    flag = True
    t1 = threading.Thread(target=thread1)
    t1.start()

def thread1():
    while flag:
        start()

def stop_button():
    flag = false

推荐阅读