How to clean up after subprocess.Popen?
I think that's all you need:
def restart_helper(self): # kill the process if open try: self.subp.kill() except OSError: # can't kill a dead proc pass self.start_helper() # the wait comes after you opened the process # if you want to know how the process ended you can add # > if self.subp.wait() != 0: # usually a process that exits with 0 had no errors self.subp.wait()
As far as I know all file objects will be closed before the popen process gets killed.
A quick experiment shows that x = open("/etc/motd"); x = 1
cleans up after itself and leaves no open file descriptor. If you drop the last reference to a subprocess.Popen
the pipes seem to stick around. Is it possible you are re-invoking start_helper()
(or even some other Popen
) without explicitly closing and stopping the old one?