Mercurial > hg > config
changeset 596:c5537e841c78
wip
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 29 Jan 2014 11:12:09 -0800 |
parents | 4b79ee6c8539 |
children | 04dfe64d1ba7 |
files | python/multiproc.py |
diffstat | 1 files changed, 42 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/multiproc.py Wed Jan 29 11:12:09 2014 -0800 @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +""" +multiprocessing/subprocess experiments +""" + +import argparse +import os +import subprocess +import sys +import time +import tempfile + +here = os.path.dirname(os.path.realpath(__file__)) +prog = ["yes"] +timeout = 4. +sleep = 1. + +def main(args=sys.argv[1:]): + + usage = '%prog [options]' + parser = argparse.ArgumentParser(usage=usage, description=__doc__) + parser.add_argument("-t", "--time", dest="time", + type=float, default=1., + help="seconds to run for") + options = parser.parse_args(args) + + output = tempfile.SpooledTemporaryFile() + start = time.time() + proc = subprocess.Popen(prog, stdout=output) + while proc.poll() is None: + if time.time() - start > options.time: + proc.kill() + + # reset tempfile + output.seek(0) + + n_lines = len(output.read().splitlines()) + print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines)) + +if __name__ == '__main__': + main()