为了简化我所拥有的情况:我正在尝试终止线程,当它仍在
Python 2.7中运行时,我不知道该怎么做.
拿这个简单的代码:
import time
import threading
def thread1():
print "Starting thread 1"
while True:
time.sleep(0.5)
print "Working"
thread1 = threading.Thread(target=thread1, args=())
thread1.start()
time.sleep(2)
print "Killing thread 1"
thread2.stop()
print "Checking if it worked:"
print "Thread is: " + str(thread1.isAlive())
线程1继续“工作”,我试图在主线程中杀死它.有什么想法怎么做?我试过了:
threat1.terminate
threat1.stop
threat1.quit
threat1.end
这一切似乎都指出,没有办法用简单的代码行来真正阻止它.你有什么建议吗?