Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess: deleting child processes in Windows

On Windows, subprocess.Popen.terminate calls win32's TerminalProcess. However, the behavior I see is that child processes of the process I am trying to terminate are still running. Why is that? How do I ensure all child processes started by the process are killed?

like image 644
Sridhar Ratnakumar Avatar asked Aug 05 '09 00:08

Sridhar Ratnakumar


People also ask

How do you kill a process using the child process?

For killing a child process after a given timeout, we can use the timeout command. It runs the command passed to it and kills it with the SIGTERM signal after the given timeout. In case we want to send a different signal like SIGINT to the process, we can use the –signal flag.

Is subprocess a child process?

Many of these programs are easily executable as a command from the shell in Unix/Linux and command prompt on windows. Python provides a module named subprocess which lets us execute these programs by creating an independent child process.


1 Answers

By using psutil:

import psutil, os  def kill_proc_tree(pid, including_parent=True):         parent = psutil.Process(pid)     children = parent.children(recursive=True)     for child in children:         child.kill()     gone, still_alive = psutil.wait_procs(children, timeout=5)     if including_parent:         parent.kill()         parent.wait(5)  me = os.getpid() kill_proc_tree(me) 
like image 93
Giampaolo Rodolà Avatar answered Sep 20 '22 13:09

Giampaolo Rodolà