# HG changeset patch # User Jeff Hammel # Date 1391114745 28800 # Node ID 782dc37492c488ff92e73447a940c66b1e10c28a # Parent e21c49e32b956cd3df09974aa026a6d669b67d04 STUB: python/multiproc.py diff -r e21c49e32b95 -r 782dc37492c4 python/multiproc.py --- a/python/multiproc.py Wed Jan 29 13:30:13 2014 -0800 +++ b/python/multiproc.py Thu Jan 30 12:45:45 2014 -0800 @@ -11,26 +11,44 @@ import time import tempfile -here = os.path.dirname(os.path.realpath(__file__)) -prog = ["yes"] -timeout = 4. -sleep = 1. +progs = {'yes': ["yes"], + 'ping': ['ping', 'google.com']} def main(args=sys.argv[1:]): + """CLI""" + # parse command line usage = '%prog [options]' parser = argparse.ArgumentParser(usage=usage, description=__doc__) parser.add_argument("-t", "--time", dest="time", + type=float, default=4., + help="seconds to run for") + parser.add_argument("-s", "--sleep", dest="sleep", type=float, default=1., - help="seconds to run for") + help="") options = parser.parse_args(args) + + # select program + prog = progs['ping'] + + # start the main subprocess loop + # TODO -> OO output = tempfile.SpooledTemporaryFile() start = time.time() proc = subprocess.Popen(prog, stdout=output) + location = 0 while proc.poll() is None: - if time.time() - start > options.time: + curr_time = time.time() + run_time = curr_time - start + if run_time > options.time: proc.kill() + if options.sleep: + time.sleep(options.sleep) + output.seek(location) + read = output.read() + location += len(read) + print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10)) # reset tempfile output.seek(0)