os.wait() losing child?

Jason Zheng Xin.Zheng at jpl.nasa.gov
Tue Jul 10 19:52:58 EDT 2007


This may be a silly question but is possible for os.wait() to lose track 
of child processes? I'm running Python 2.4.4 on Linux kernel 2.6.20 
(i686), gcc4.1.1, and glibc-2.5.

Here's what happened in my situation. I first created a few child 
processes with Popen, then in a while(True) loop wait on any of the 
child process to exit, then restart a child process:

import os
from subprocess import Popen

pids = {}

for i in xrange(3):
   p = Popen('sleep 1', shell=True, cwd='/home/user', 
stdout=file(os.devnull,'w'))
   pids[p.pid] = i

while (True):
   pid = os.wait()
   i = pids[pid]
   del pids[pid]
   print "Child Process %d terminated, restarting" % i
   if (someCondition):
     break
   p = Popen('sleep 1', shell=True, cwd='/home/user', 
stdout=file(os.devnull,'w'))
   pids[p.pid] = i

As I started to run this program, soon I discovered that some of the 
processes stopped showing up, and eventually os.wait() will give an 
error saying that there's no more child process to wait on. Can anyone 
tell me what I did wrong?



More information about the Python-list mailing list