In python when you want to kill a process immediately, you can do it by calling terminate method.

But what about thread? there is no terminate method for thread.

By using ctype library you can make a way to send termination request to a thread.

import ctypes

def terminate(t):
  """Terminate thread.

  :param threading.Thread t: thread object
  exec = ctypes.py_object(SystemExit)
  res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(t.ident), exec))
  if res == 0:
    print("thread not found!")
  elif res > 1:
    ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(t.ident), None))

Note: It’s not a certain way and it might that your thread could not terminate as well as process.

All rights reserved ©