Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么线不停?_Python_Multithreading - Fatal编程技术网

Python 为什么线不停?

Python 为什么线不停?,python,multithreading,Python,Multithreading,在start\u thread方法中启动的线程不会停止。为什么? import time import threading cont_running = True def start_thread(): threading.Thread(target=run).start() def stop_thread(): cont_running = False def run(): while cont_running: print 'Thread ru

start\u thread
方法中启动的线程不会停止。为什么?

import time
import threading

cont_running = True

def start_thread():
    threading.Thread(target=run).start()

def stop_thread():
    cont_running = False

def run():
    while cont_running:
        print 'Thread running : ' + str(cont_running)
        time.sleep(0.2)
    print 'Thread ended'

start_thread()
time.sleep(2)
stop_thread()
stop\u thread()
中,赋值语句创建一个名为
cont\u running
的局部变量。此局部变量与同名的全局变量无关

试试这个:

def stop_thread():
    global cont_running
    cont_running = False
stop\u thread()
中,赋值语句创建一个名为
cont\u running
的局部变量。此局部变量与同名的全局变量无关

试试这个:

def stop_thread():
    global cont_running
    cont_running = False