有人可以在os.wait()中解释错误原因吗?

时间:2016-12-24 17:49:12

标签: python multiprocessing fork

我有fork子进程的这段代码分别自上而下有wait()函数,但是当我运行脚本时出现错误。

import os

reply = int(input("Enter no of proc:   "))
pid = os.fork()

for i in range(reply):

    if pid == 0:
        pid = os.fork()

    else:
        os.wait()

        print(i,os.getpid(), os.getppid())

3的输出是:

Enter no of proc:   3
2 44070 44069
1 44069 44068
Traceback (most recent call last):
  File "/Users/saman/PycharmProjects/Syslab/test.py", line 12, in <module>
    os.wait()
ChildProcessError: [Errno 10] No child processes
Traceback (most recent call last):
  File "/Users/saman/PycharmProjects/Syslab/test.py", line 12, in <module>
    os.wait()
ChildProcessError: [Errno 10] No child processes
0 44068 20928

我不明白错误!

1 个答案:

答案 0 :(得分:3)

您正在儿童中执行os.wait()。这些孩子无法等待。

我想你想做这样的事情:

import os

reply = int(input("Enter no of proc:   "))
pid = os.fork()

for i in range(reply):

    if pid == 0:
        pid = os.fork()

if pid != 0:
    os.wait()
    print(os.getpid(), os.getppid())