简体   繁体   English

无需在python中收获僵尸进程吗?

[英]Is there no need to reap zombie process in python?

It seems to me in Python, there is no need to reap zombie processes. 在Python中,似乎不需要收获僵尸进程。

For example, in the following code 例如,在以下代码中

    import multiprocessing
    import time

    def func(msg):
        time.sleep(2)
        print "done " + str(msg)

    if __name__ == "__main__":
        for i in range(10):
            p = multiprocessing.Process(target=func, args=('3'))
            p.start()
            print "child"+str(i)
        print "parent"
        time.sleep(100)

When all the child process exit, the parent process is still running and at this time, I checked the process using ps -ef and I noticed there is no defunct process. 当所有子进程都退出时,父进程仍在运行,这时,我使用ps -ef检查了该进程,并发现没有失效的进程。

Does this mean that in Python, there is no need to reap zombie process? 这是否意味着在Python中无需获得僵尸进程?

Those processes are not actually zombies since they should terminate successfully. 这些进程实际上不是僵尸,因为它们应该成功终止。 You could set the child processes to be deamonic so they'll terminate if the main process terminates. 您可以将子进程设置为消声级,这样,如果主进程终止,它们将终止。

After having a look to the library - especially to multiprocessing/process.py -, I see that 看了一下库-特别是multiprocessing/process.py ,我看到了

  1. in Process.start() , there is a _current_process._children.add(self) which adds the started process to a list/set/whatever, Process.start() ,有一个_current_process._children.add(self)将启动的进程添加到列表/集合/任何内容,
  2. a few lines above, there is a _cleanup() which polls and discards terminated processes, removing zombies. 在上面的几行中,有一个_cleanup()它将轮询并丢弃终止的进程,从而删除僵尸。

But that doesn't explain why your code doesn't produce zombies, as the childs wait a while befor terminating, so that the parent's start() calls don't notice that yet. 但这并不能解释为什么您的代码不会产生僵尸,因为孩子们会在等待终止之前等待一会儿,因此父级的start()调用尚未注意到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM