Python结束线程的方法
目录
Python结束线程的方法
import inspect
import ctypes
def _async_raise(tid, exctype):
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("Invalid tid")
elif res != 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise RuntimeError("PyThreadState_SetAsyncExc failed")
def stop_thread(thread):
self._async_raise(thread.ident, SystemExit)
以上代码由一个朋友提供~