Search code examples
pythonwindowssubprocesspopenkill

Popen subprocess creates two processes


I am using this code to make a killswitch for my program

killSwitch = Popen(['killswitch.exe', str(pid)])

it feeds in the pid of the main thread, if the main thread becomes unresponsive i can use a keybind to kill it. However when closing normally, without running the killswitch, it never closes. So i need to handle the process, but using popen creates two processes and only reports back one, doing killSwitch.kill() doesn't work because one process is still left open. I also do not want to taskkill killSwitch.exe because if other instanaces are open i do not want to close the killSwitch for the other instances. Is there a way to make it so only one process opens or close both the processes?


Solution

  • I was able to solve my problem by using

    os.system(f'taskkill /f /t /pid {killSwitch.pid}')

    This kills the whole process tree although I only have one pid without interrupting other instances.