Mercurial > hg > config
comparison python/multiproc.py @ 615:6db42d965f0e
STUB: python/multiproc.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 02 Feb 2014 18:44:05 -0800 |
parents | f905378fcee0 |
children | 41133baea90d |
comparison
equal
deleted
inserted
replaced
614:f905378fcee0 | 615:6db42d965f0e |
---|---|
121 | 121 |
122 | 122 |
123 def main(args=sys.argv[1:]): | 123 def main(args=sys.argv[1:]): |
124 """CLI""" | 124 """CLI""" |
125 | 125 |
126 description = """demonstration of how to do things with subprocess""" | |
127 | |
126 # available programs | 128 # available programs |
127 progs = {'yes': ["yes"], | 129 progs = {'yes': ["yes"], |
128 'ping': ['ping', 'google.com']} | 130 'ping': ['ping', 'google.com']} |
129 | 131 |
130 # parse command line | 132 # parse command line |
131 parser = argparse.ArgumentParser(description=__doc__) | 133 parser = argparse.ArgumentParser(description=description) |
132 parser.add_argument("-t", "--time", dest="time", | 134 parser.add_argument("-t", "--time", dest="time", |
133 type=float, default=4., | 135 type=float, default=4., |
134 help="seconds to run for (or <= 0 for forever)") | 136 help="seconds to run for (or <= 0 for forever)") |
135 parser.add_argument("-s", "--sleep", dest="sleep", | 137 parser.add_argument("-s", "--sleep", dest="sleep", |
136 type=float, default=1., | 138 type=float, default=1., |
139 choices=progs.keys(), default='ping', | 141 choices=progs.keys(), default='ping', |
140 help="subprocess to run") | 142 help="subprocess to run") |
141 parser.add_argument("--list-programs", dest='list_programs', | 143 parser.add_argument("--list-programs", dest='list_programs', |
142 action='store_true', default=False, | 144 action='store_true', default=False, |
143 help="list available programs") | 145 help="list available programs") |
146 parser.add_argument("--wait", dest='wait', | |
147 action='store_true', default=False, | |
148 help="run with ``.wait()`` and a callback") | |
149 parser.add_argument("--callback", dest='callback', | |
150 action='store_true', default=False, | |
151 help="run with polling and a callback") | |
144 options = parser.parse_args(args) | 152 options = parser.parse_args(args) |
145 | 153 |
146 # list programs | 154 # list programs |
147 if options.list_programs: | 155 if options.list_programs: |
148 for key in sorted(progs.keys()): | 156 for key in sorted(progs.keys()): |
153 prog = progs[options.program] | 161 prog = progs[options.program] |
154 | 162 |
155 # start process | 163 # start process |
156 proc = Process(prog) | 164 proc = Process(prog) |
157 | 165 |
158 # callback for output processing | 166 # demo function for processing output |
159 def process_output(output): | 167 def output_processor(output): |
160 print ('[{}] {}{}'.format(proc.runtime(), | 168 print ('[{}]:\n{}\n{}'.format(proc.runtime(), |
161 output.upper(), | 169 output.upper(), |
162 '-==-'*10)) | 170 '-==-'*10)) |
163 | 171 if options.callback: |
164 # start the main subprocess loop | 172 process_output = output_processor |
165 while proc.poll(process_output) is None: | 173 else: |
166 | 174 process_output = None |
167 if options.time > 0 and proc.runtime() > options.time: | 175 |
168 proc.kill() | 176 if options.wait: |
169 | 177 # wait for being done |
170 if options.sleep: | 178 proc.wait(maxtime=options.time, sleep=options.sleep, process_output=output_processor) |
171 time.sleep(options.sleep) | 179 else: |
172 | 180 # start the main subprocess loop |
173 # wait for being done | 181 while proc.poll(process_output) is None: |
174 #proc.wait(maxtime=options.time, sleep=options.sleep, process_output=process_output) | 182 |
183 if options.time > 0 and proc.runtime() > options.time: | |
184 proc.kill() | |
185 | |
186 if options.sleep: | |
187 time.sleep(options.sleep) | |
188 | |
189 if process_output is not None: | |
190 # process the output with ``.read()`` call | |
191 proc.read(output_processor) | |
175 | 192 |
176 # correctness tests | 193 # correctness tests |
177 assert proc.end is not None | 194 assert proc.end is not None |
178 | 195 |
179 # print summary | 196 # print summary |
180 output = proc.output | 197 output = proc.output |
181 n_lines = len(output.splitlines()) | 198 n_lines = len(output.splitlines()) |
182 print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines)) | 199 print ("{}: {} lines, ran for {} seconds".format(subprocess.list2cmdline(prog), n_lines, proc.runtime())) |
183 | 200 |
184 if __name__ == '__main__': | 201 if __name__ == '__main__': |
185 main() | 202 main() |