24 lines
655 B
Python
24 lines
655 B
Python
import ctypes
|
|
|
|
set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
|
|
|
|
|
|
def killthread(thread, exc=KeyboardInterrupt):
|
|
if not thread.is_alive():
|
|
return
|
|
|
|
ident = ctypes.c_long(thread.ident)
|
|
exc = ctypes.py_object(exc)
|
|
|
|
res = set_async_exc(ident, exc)
|
|
if res == 0:
|
|
raise ValueError(f"thread {thread} does not exist")
|
|
elif res > 1:
|
|
# if return value is greater than one, you are in trouble.
|
|
# you should call it again with exc=NULL to revert the effect.
|
|
set_async_exc(ident, None)
|
|
raise SystemError(f"PyThreadState_SetAsyncExc on thread {thread} failed with return value {res}")
|
|
|
|
|
|
|