os.wait() losing child?

J

Jason Zheng

Hrvoje said:
Jason Zheng said:
Hrvoje said:
greg wrote:
Actually, it's not that bad. _cleanup only polls the instances that
are no longer referenced by user code, but still running. If you hang
on to Popen instances, they won't be added to _active, and __init__
won't reap them (_active is only populated from Popen.__del__).
Perhaps that's the difference between Python 2.4 and 2.5. [...]
Nope it still doesn't work. I'm running python 2.4.4, tho.

That explains it, then, and also why greg's code didn't work. You
still have the option to try to run 2.5's subprocess.py under 2.4.
Is it more convenient to just inherit the Popen class? I'm concerned
about portability of my code. It will be run on multiple machines with
mixed Python 2.4 and 2.5 environments.
 
H

Hrvoje Niksic

Jason Zheng said:
Is it more convenient to just inherit the Popen class?

You'd still need to change its behavior to not call _cleanup. For
example, by removing "your" instances from subprocess._active before
chaining up to Popen.__init__.
I'm concerned about portability of my code. It will be run on
multiple machines with mixed Python 2.4 and 2.5 environments.

I don't think there is a really clean way to handle this.
 
J

Jason Zheng

Hrvoje said:
I don't think there is a really clean way to handle this.

I think the following might just work, albeit not "clean":

#!/usr/bin/python

import os,subprocess
from subprocess import Popen

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

def launch(i):
p = Popen('sleep 1', shell=True, cwd='/home',
stdout=file(os.devnull,'w'))
pids[p.pid] = p, i
if p in subprocess._active:
subprocess._active.remove(p)
print "Starting child process %d (%d)" % (i,p.pid)

for i in xrange(3):
launch(i)

while (True):
pid, ignored = os.wait()
try:
p, i = pids[pid]
except KeyError:
# not one of ours
continue
del pids[pid]
counts += 1

#terminate if count>10
if (counts==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
launch(i)
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top