Python 中fork进程的问题

Python代码如下:

 for i in range(0,2):
    print("#i=%s, who:%s" % (i, os.getpid()))
    pid = os.fork()
    if pid == 0:
        print("i=%s in child process, pid=%s, parent_pid=%s" % (i, os.getpid(), os.getppid()))
    elif pid > 0:
        print("i=%s in parent process, pid=%s, child_pid=%s" % (i, os.getpid(), pid))
    else:
        print("Error")

运行结果:

 Process (3159) start...
#i=0, who:3159
i=0 in parent process, pid=3159, child_pid=3161
#i=1, who:3159
i=1 in parent process, pid=3159, child_pid=3162
i=1 in child process, pid=3162, parent_pid=3159
i=0 in child process, pid=3161, parent_pid=3159
#i=1, who:3161
i=1 in parent process, pid=3161, child_pid=3163
#i=0, who:3160
i=0 in parent process, pid=3160, child_pid=3164
#i=1, who:3160
i=1 in parent process, pid=3160, child_pid=3165
i=1 in child process, pid=3163, parent_pid=3161
i=0 in child process, pid=3164, parent_pid=3160
#i=1, who:3164
i=1 in parent process, pid=3164, child_pid=3166
i=1 in child process, pid=3165, parent_pid=3160
i=1 in child process, pid=3166, parent_pid=3164

求大神指点下进程创建的过程,为什么是6次。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^