import psutil import os import ctypes,inspect import traceback 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: ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError("PyThreadState_SetAsyncExc failed") def stop_thread(thread): if isinstance(thread,(int)): _ident = thread else: _ident = thread.ident print(_ident) _async_raise(_ident, SystemExit) for pid in [124564]: try: proc = psutil.Process(pid) exeFile = os.path.basename(proc.exe()) threads = psutil._psutil_windows.proc_threads(pid) times = 0 for thread in threads: print(thread) stop_thread(thread[0]) for timeUsed in thread[1:]: times += timeUsed print('='*20) print('Exe file:', os.path.basename(proc.exe())) print('Number of threads:', len(threads)) print('Time used:', times) except: traceback.print_exc()