Forking python, defunct child

徘徊边缘 提交于 2019-12-02 23:04:40

To clear the child process in Unix you need to wait on the child, check one of the os.wait(), os.waitpid(), os.wait3() or os.wait4() at http://docs.python.org/2/library/os.html#os.wait

As to why this is so, this is a design decision of Unix. The child process keeps its return value in its process state, if it was to disappear you'll have no return value. The os.wait() also returns to you the return value and then the child process is released and all associated resources are released.

I just had a similar problem: A process started by spawnl, which might end or might need to be terminated at a specific point. My solution to not have all the zombie processes was

def cleanup_subprocesses(self, pid):
  try:
    os.kill(pid, signal.SIGKILL)
  except OSError:
    pass
  os.waitpid(self._pid, 0)

If the process did not end in time, it gets killed, in any case, the waitpid-command is executed.

This does obviously not help, if there is no good point in your program, where you know, that you don’t need the process anymore.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!