changeset 599:782dc37492c4

STUB: python/multiproc.py
author Jeff Hammel <k0scist@gmail.com>
date Thu, 30 Jan 2014 12:45:45 -0800
parents e21c49e32b95
children a77f7022cc06
files python/multiproc.py
diffstat 1 files changed, 24 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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)