# HG changeset patch # User Jeff Hammel # Date 1391394315 28800 # Node ID f905378fcee02f8c8858738b62b0defcfaeaf2e3 # Parent bf3f8bc6d1b33a0033a8a440067f373f099f0c23 STUB: python/multiproc.py diff -r bf3f8bc6d1b3 -r f905378fcee0 python/multiproc.py --- a/python/multiproc.py Sun Feb 02 16:14:17 2014 -0800 +++ b/python/multiproc.py Sun Feb 02 18:25:15 2014 -0800 @@ -52,21 +52,27 @@ # read final output self.read(process_output) - # reset output buffer + # reset output buffer location self.output_buffer.seek(0) # set end time self.end = time.time() - def poll(self): - return subprocess.Popen.poll(self) + def poll(self, process_output=None): + + if process_output is not None: + self.read(process_output) # read from output buffer + poll = subprocess.Popen.poll(self) + if poll is not None: + self._finalize(process_output) + return poll def wait(self, maxtime=None, sleep=1., process_output=None): """ maxtime -- timeout in seconds sleep -- number of seconds to sleep between polling """ - while self.poll() is None: + while self.poll(process_output) is None: # check for timeout curr_time = time.time() @@ -76,9 +82,6 @@ self._finalize(process_output) return - # read from output buffer - self.read(process_output) - # naptime if sleep: time.sleep(sleep) @@ -128,7 +131,7 @@ parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("-t", "--time", dest="time", type=float, default=4., - help="seconds to run for") + help="seconds to run for (or <= 0 for forever)") parser.add_argument("-s", "--sleep", dest="sleep", type=float, default=1., help="sleep this number of seconds between polling") @@ -154,40 +157,26 @@ # callback for output processing def process_output(output): - print ('[{}] {}\n{}'.format(proc.runtime(), - output.upper(), - '-==-'*10)) - - - # LEGACY: output = tempfile.SpooledTemporaryFile() - # start = time.time() - # proc = subprocess.Popen(prog, stdout=output) - # location = 0 - -# start the main subprocess loop -# while proc.poll() is None: + print ('[{}] {}{}'.format(proc.runtime(), + output.upper(), + '-==-'*10)) - # LEGACY: - # curr_time = time.time() - # run_time = curr_time - start - # if run_time > options.time: - # proc.kill() - + # start the main subprocess loop + while proc.poll(process_output) is None: - # output.seek(location) - # read = output.read() - # location += len(read) - # print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10)) - # if options.sleep: - # time.sleep(options.sleep) + if options.time > 0 and proc.runtime() > options.time: + proc.kill() - # # reset tempfile - # output.seek(0) + if options.sleep: + time.sleep(options.sleep) # wait for being done - proc.wait(maxtime=options.time, sleep=options.sleep, process_output=process_output) + #proc.wait(maxtime=options.time, sleep=options.sleep, process_output=process_output) - # finalization + # correctness tests + assert proc.end is not None + + # print summary output = proc.output n_lines = len(output.splitlines()) print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines))