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

J

JohnMudd

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)
 
H

Hrvoje Niksic

JohnMudd said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top