根据Thomas Watnedal的回答中的提示,他指出示例中实际上只是外壳被杀死,我根据Mark
Hammond的PyWin32库中给出的示例,安排了以下函数来解决我的方案的问题。 :
procname是在任务管理器中看到的没有扩展名的进程的名称,例如FFMPEG.EXE将是killProcName(“
FFMPEG”)。请注意,该函数相当慢,因为它对所有当前正在运行的进程进行枚举,因此结果不是即时的。
import win32apiimport win32pdhutilimport win32condef killProcName(procname): """Kill a running process by name. Kills first process with the given name.""" try: win32pdhutil.GetPerformanceAttributes("Process", "ID Process", procname) except: pass pids = win32pdhutil.FindPerformanceAttributesByName(procname) # If _my_ pid in there, remove it! try: pids.remove(win32api.GetCurrentProcessId()) except ValueError: pass handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, pids[0]) win32api.TerminateProcess(handle, 0) win32api.CloseHandle(handle)


