Mercurial > hg > config
comparison python/multiproc.py @ 614:f905378fcee0
STUB: python/multiproc.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 02 Feb 2014 18:25:15 -0800 |
parents | bf3f8bc6d1b3 |
children | 6db42d965f0e |
comparison
equal
deleted
inserted
replaced
613:bf3f8bc6d1b3 | 614:f905378fcee0 |
---|---|
50 """internal function to finalize""" | 50 """internal function to finalize""" |
51 | 51 |
52 # read final output | 52 # read final output |
53 self.read(process_output) | 53 self.read(process_output) |
54 | 54 |
55 # reset output buffer | 55 # reset output buffer location |
56 self.output_buffer.seek(0) | 56 self.output_buffer.seek(0) |
57 | 57 |
58 # set end time | 58 # set end time |
59 self.end = time.time() | 59 self.end = time.time() |
60 | 60 |
61 def poll(self): | 61 def poll(self, process_output=None): |
62 return subprocess.Popen.poll(self) | 62 |
63 if process_output is not None: | |
64 self.read(process_output) # read from output buffer | |
65 poll = subprocess.Popen.poll(self) | |
66 if poll is not None: | |
67 self._finalize(process_output) | |
68 return poll | |
63 | 69 |
64 def wait(self, maxtime=None, sleep=1., process_output=None): | 70 def wait(self, maxtime=None, sleep=1., process_output=None): |
65 """ | 71 """ |
66 maxtime -- timeout in seconds | 72 maxtime -- timeout in seconds |
67 sleep -- number of seconds to sleep between polling | 73 sleep -- number of seconds to sleep between polling |
68 """ | 74 """ |
69 while self.poll() is None: | 75 while self.poll(process_output) is None: |
70 | 76 |
71 # check for timeout | 77 # check for timeout |
72 curr_time = time.time() | 78 curr_time = time.time() |
73 run_time = self.runtime() | 79 run_time = self.runtime() |
74 if maxtime is not None and run_time > maxtime: | 80 if maxtime is not None and run_time > maxtime: |
75 self.kill() | 81 self.kill() |
76 self._finalize(process_output) | 82 self._finalize(process_output) |
77 return | 83 return |
78 | |
79 # read from output buffer | |
80 self.read(process_output) | |
81 | 84 |
82 # naptime | 85 # naptime |
83 if sleep: | 86 if sleep: |
84 time.sleep(sleep) | 87 time.sleep(sleep) |
85 | 88 |
126 | 129 |
127 # parse command line | 130 # parse command line |
128 parser = argparse.ArgumentParser(description=__doc__) | 131 parser = argparse.ArgumentParser(description=__doc__) |
129 parser.add_argument("-t", "--time", dest="time", | 132 parser.add_argument("-t", "--time", dest="time", |
130 type=float, default=4., | 133 type=float, default=4., |
131 help="seconds to run for") | 134 help="seconds to run for (or <= 0 for forever)") |
132 parser.add_argument("-s", "--sleep", dest="sleep", | 135 parser.add_argument("-s", "--sleep", dest="sleep", |
133 type=float, default=1., | 136 type=float, default=1., |
134 help="sleep this number of seconds between polling") | 137 help="sleep this number of seconds between polling") |
135 parser.add_argument("-p", "--prog", dest='program', | 138 parser.add_argument("-p", "--prog", dest='program', |
136 choices=progs.keys(), default='ping', | 139 choices=progs.keys(), default='ping', |
152 # start process | 155 # start process |
153 proc = Process(prog) | 156 proc = Process(prog) |
154 | 157 |
155 # callback for output processing | 158 # callback for output processing |
156 def process_output(output): | 159 def process_output(output): |
157 print ('[{}] {}\n{}'.format(proc.runtime(), | 160 print ('[{}] {}{}'.format(proc.runtime(), |
158 output.upper(), | 161 output.upper(), |
159 '-==-'*10)) | 162 '-==-'*10)) |
160 | 163 |
164 # start the main subprocess loop | |
165 while proc.poll(process_output) is None: | |
161 | 166 |
162 # LEGACY: output = tempfile.SpooledTemporaryFile() | 167 if options.time > 0 and proc.runtime() > options.time: |
163 # start = time.time() | 168 proc.kill() |
164 # proc = subprocess.Popen(prog, stdout=output) | |
165 # location = 0 | |
166 | 169 |
167 # start the main subprocess loop | 170 if options.sleep: |
168 # while proc.poll() is None: | 171 time.sleep(options.sleep) |
169 | |
170 # LEGACY: | |
171 # curr_time = time.time() | |
172 # run_time = curr_time - start | |
173 # if run_time > options.time: | |
174 # proc.kill() | |
175 | |
176 | |
177 # output.seek(location) | |
178 # read = output.read() | |
179 # location += len(read) | |
180 # print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10)) | |
181 # if options.sleep: | |
182 # time.sleep(options.sleep) | |
183 | |
184 # # reset tempfile | |
185 # output.seek(0) | |
186 | 172 |
187 # wait for being done | 173 # wait for being done |
188 proc.wait(maxtime=options.time, sleep=options.sleep, process_output=process_output) | 174 #proc.wait(maxtime=options.time, sleep=options.sleep, process_output=process_output) |
189 | 175 |
190 # finalization | 176 # correctness tests |
177 assert proc.end is not None | |
178 | |
179 # print summary | |
191 output = proc.output | 180 output = proc.output |
192 n_lines = len(output.splitlines()) | 181 n_lines = len(output.splitlines()) |
193 print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines)) | 182 print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines)) |
194 | 183 |
195 if __name__ == '__main__': | 184 if __name__ == '__main__': |