472,093 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,093 software developers and data experts.

subprocess.Popen(cmd) question

Hello,

I'm starting some subprocesses inside a loop. The processes run
independent and dont need any communication between each other. Due to
memory issues I need to limit the number of running processes to around
10. How can I insert a break into my loop to wait until some processes
are finished?

Some minimal examplecode:

import subprocess
for i in range(0,100):
cmd='ping localhost'
p=subprocess.Popen(cmd)
p.wait()

Thanks for any ideas.

Wolfgang

Aug 10 '07 #1
1 1994
WolfgangZ wrote:
I'm starting some subprocesses inside a loop. The processes run
independent and dont need any communication between each other. Due to
memory issues I need to limit the number of running processes to around
10. How can I insert a break into my loop to wait until some processes
are finished?

Some minimal examplecode:

import subprocess
for i in range(0,100):
cmd='ping localhost'
p=subprocess.Popen(cmd)
p.wait()
Just polling the processes may be good enough:

import random
import subprocess
import sys
import time
from itertools import islice
from functools import partial

PROCESSES = 100
SIMULTANEOUS = 10

def info(*args):
print >sys.stderr, " ".join(str(a) for a in args)

class Process(object):
def __init__(self, index):
self.index = index
info("starting process #%d" % index)
self.process = subprocess.Popen(["ping", "localhost", "-w", "%d" %
random.randrange(1, 6)])
def __nonzero__(self):
running = self.process.poll() is None
if not running:
# XXX ugly side effect
info("process #%d terminated" % self.index)
return running

def processes():
for i in range(PROCESSES):
yield partial(Process, i)

def main():
starters = processes()
running = [sp() for sp in islice(starters, SIMULTANEOUS)]
while running:
before = len(running)
running = [p for p in running if p]
after = len(running)
if before == after:
info("waiting")
time.sleep(1)
else:
running.extend(sp() for sp in islice(starters, SIMULTANEOUS -
len(running)))
info("that's all, folks")

if __name__ == "__main__":
main()

Peter
Aug 10 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jim Benson | last post: by
6 posts views Thread by Ernesto | last post: by
3 posts views Thread by George Sakkis | last post: by
12 posts views Thread by Eric_Dexter | last post: by
5 posts views Thread by Robert Latest | last post: by
1 post views Thread by Mark Shewfelt | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.