# HG changeset patch # User Jeff Hammel # Date 1391022729 28800 # Node ID c5537e841c78e5cc3c051a548e8b4524c4c94f02 # Parent 4b79ee6c8539b185642e02a7f37b1dc5bb0fb254 wip diff -r 4b79ee6c8539 -r c5537e841c78 python/multiproc.py --- /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()