import inspect
import ctypes
import threading
import time
def stop_threading_fun(tid, exctype=SystemExit):
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:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
def start_threading_fun():
t = threading.Thread(target=loop_fun, args=())
t.start()
tid = t.ident
return tid
def loop_fun():
for i in range(100):
print(i)
time.sleep(1)
if __name__ == '__main__':
tid = start_threading_fun()
print(tid)
time.sleep(15)
stop_threading_fun(tid)
time.sleep(100)