os.wait() losing child?

Jason Zheng Xin.Zheng at jpl.nasa.gov
Wed Jul 11 03:05:52 CEST 2007


Hate to reply to my own thread, but this is the working program that can 
demonstrate what I posted earlier:

import os
from subprocess import Popen

pids = {}
counts = [0,0,0]

for i in xrange(3):
   p = Popen('sleep 1', shell=True, cwd='/home', 
stdout=file(os.devnull,'w'))
   pids[p.pid] = i
   print "Starting child process %d (%d)" % (i,p.pid)

while (True):
   (pid,exitstat) = os.wait()
   i = pids[pid]
   del pids[pid]
   counts[i]=counts[i]+1

   #terminate if count>10
   if (counts[i]==10):
     print "Child Process %d terminated." % i
     if reduce(lambda x,y: x and (y>=10), counts):
       break
     continue

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



More information about the Python-list mailing list