“不要试图用强制方法杀掉一个python线程,这从服务设计上就存在不合理性。 多线程本用来任务的协作并发,如果你使用强制手段干掉线程,那么很大几率出现意想不到的bug。”
话虽然这样说,但是有时候就有这样的需求,可以python本身没有提供这样的API,所以没办法在网上找了一圈,发现了两种方法。如下:
方法一:
利用setDaemon(True)这个函数的特性,特性如下:主线程A中,创建了子线程B,并且在主线程A中调用了B.setDaemon(),这个的意思是,把主线程A设置为守护线程,这时候,要是主线程A执行结束了,就不管子线程B是否完成,一并和主线程A退出。
但是如果要做到主线程不结束,但还是要强行结束子线程。所以我就突发奇想,如果我把要杀死的子线程看做是孙线程,给一个标志位给子线程,主线程改变标志位,子线程检查到就break自己,这样孙线程不就结束了,主线程仍在运行。然而,想法很美好。。。先上代码。
import threading flag = 0 # 为线程定义一个函数 def print_time(): def printOne(): while 1: print(111111111111) print(222222222222) print(333333333333) print(444444444444) print(555555555555) print(666666666666) th1 = threading.Thread(target=printOne) th1.setDaemon(True) th1.start() while 1: if flag: print("正在停止这个程序!!!") break i=5 if i == 5: th = threading.Thread(target=print_time) th.start() flag=1 th.join() print("++++++++++++++++++++++++++++++++++++++++++++++++++") while 1: pass
执行代码,会发现孙线程并没有结束。很简单,因为孙线程它会等主线程结束,它才结束。去掉最后两行代码,孙线程就会结束,但这也是等主线程结束的。所以方法一不满足需求。
方法二:
使用ctypes强行杀掉线程
import threading import time import inspect import ctypes def _async_raise(tid, exctype): """raises the exception, performs cleanup if needed""" tid = ctypes.c_long(tid) if not inspect.isclass(exctype): exctype = type(exctype) res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) if res == 0: raise ValueError("invalid thread id") elif res != 1: # """if it returns a number greater than one, you're in trouble, # and you should call it again with exc=NULL to revert the effect""" ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError("PyThreadState_SetAsyncExc failed") def stop_thread(thread): _async_raise(thread.ident, SystemExit) def print_time(): while 2: print(111111111111) print(222222222222) print(333333333333) print(444444444444) print(555555555555) print(666666666666) if __name__ == "__main__": t = threading.Thread(target=print_time) t.start() stop_thread(t) print("stoped") while 1: pass
这个方法是在网上找的,推荐一下,非常干净利索的干掉了子线程。