Netro
Netro

Reputation: 7297

OSError in os.wait in python

Hi I m trying to execute small code in python , it is giving OS error.

>>> import os
>>> def child():
...     pid = os.fork()
...     if not pid:
...             for i in range(5):
...                     print i
...     return os.wait()
...
>>> child()
0
1
2
3
4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in child
OSError: [Errno 10] No child processes

I m not able to figure out why it is giving OSError. I googled it and it is noted as bug for python 2.6 or before. I'm using python2.7.

Upvotes: 0

Views: 1359

Answers (1)

stanwise
stanwise

Reputation: 2412

You missed an else. Thus, you are calling os.wait() in children processes (who have no children of their own, hence the error).

Corrected code below:

import os
def child():
    pid = os.fork()
    if not pid:
            for i in range(5):
                    print i
    else:
        return os.wait()

child()

Upvotes: 3

Related Questions