os.popen() interferes with os.fork()?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JohnMudd

    os.popen() interferes with os.fork()?

    On Linux I get a "close failed: [Errno 10] No child processes" msg for
    each fork. But only if I have a pipe open. I don't understand the
    connection between the popen and the forks. Am I doing something
    wrong?


    #! /usr/bin/env python

    import os
    import sys
    import time

    p = os.popen('top -b -d1')

    n = 3
    t1 = time.time()
    for i in range(n):
    pid = os.fork()
    if pid == 0:
    sys.exit(0)
    delay = time.time() - t1
    print 'rate: %.1f fork/sec' % (n/delay)
  • Hrvoje Niksic

    #2
    Re: os.popen() interferes with os.fork()?

    JohnMudd <JohnBMudd@gmai l.comwrites:
    On Linux I get a "close failed: [Errno 10] No child processes" msg
    for each fork. But only if I have a pipe open. I don't understand
    the connection between the popen and the forks. Am I doing
    something wrong?
    Yes: don't use sys.exit, use os._exit. This is not specific to
    Python; in C you'd also need to use _exit(2) in preference to exit(3)
    to exit a forked process.

    With sys.exit(0) changed to os._exit(0), the program outputs:

    rate: 2962.1 fork/sec

    Comment

    Working...