python 强制停止线程

 1 # coding=utf-8
 2 import threading
 3 import time
 4 
 5 
 6 class CountdownTask:
 7     def __init__(self):
 8         self._running = True
 9 
10     def terminate(self):
11         self._running = False
12 
13     def run(self, n):
14         while self._running:
15             # 将要执行的任务放在此处
16             # 示例
17             print("T-minus  {}
".format(n))
18             n -= 1
19             time.sleep(100)
20             # end
21 
22 
23 # 示例
24 # stop threading
25 countdownTask = CountdownTask()
26 th = threading.Thread(target=countdownTask.run, args=(10,))  # args可以给run传参
27 th.start()
28 countdownTask.terminate()  # Signal termination
29 # end
原文地址:https://www.cnblogs.com/coodyz/p/11396497.html