Mercurial > hg > config
comparison python/multiproc.py @ 606:5b561c7b2005
STUB: python/multiproc.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 02 Feb 2014 11:07:04 -0800 |
parents | bd30deecdf4a |
children | 567492ef7b57 |
comparison
equal
deleted
inserted
replaced
605:ab9145ded910 | 606:5b561c7b2005 |
---|---|
9 import subprocess | 9 import subprocess |
10 import sys | 10 import sys |
11 import time | 11 import time |
12 import tempfile | 12 import tempfile |
13 | 13 |
14 string = (str, unicode) | |
15 | |
14 class Process(subprocess.Popen): | 16 class Process(subprocess.Popen): |
15 """why would you name a subprocess object Popen?""" | 17 """why would you name a subprocess object Popen?""" |
16 | 18 |
17 defaults = {'buffsize': 'line buffered' | 19 # http://docs.python.org/2/library/subprocess.html#popen-constructor |
20 defaults = {'buffsize': 1, # line buffered | |
18 } | 21 } |
19 | 22 |
20 def __init__(self, *args, **kwargs): | 23 def __init__(self, command, **kwargs): |
21 self.output = tempfile.SpooledTemporaryFile() | 24 |
22 subprocess.Popen.__init__(self, *args, **kwargs) | 25 # setup arguments |
23 # TODO: finish | 26 self.command = command |
27 _kwargs = self.defaults.copy() | |
28 _kwargs.update(kwargs) | |
29 | |
30 # output buffer | |
31 self.output_buffer = tempfile.SpooledTemporaryFile() | |
32 self.location = 0 | |
33 self.output = [] | |
34 _kwargs['stdout'] = self.output_buffer | |
35 | |
36 # launch subprocess | |
37 self.start = time.time() | |
38 subprocess.Popen.__init__(self, *args, **_kwargs) | |
39 | |
40 def wait(self, maxtime=None, sleep=1.): | |
41 """ | |
42 maxtime -- timeout in seconds | |
43 sleep -- number of seconds to sleep between polling | |
44 """ | |
45 while self.poll() is None: | |
46 | |
47 # check for timeout | |
48 curr_time = time.time() | |
49 run_time = curr_time - self.start | |
50 if run_time > maxtime: | |
51 # TODO: read from output | |
52 return process.kill() | |
53 | |
54 # naptime | |
55 if sleep: | |
56 time.sleep(sleep) | |
57 | |
58 # read from output buffer | |
59 self.output_buffer.seek(self.location) | |
60 read = self.output_buffer.read() | |
61 self.location += len(read) | |
62 | |
63 def commandline(self): | |
64 """returns string of command line""" | |
65 | |
66 if isinstance(self.command, string): | |
67 return self.command | |
68 return subprocess.list2cmdline(self.command) | |
69 | |
70 __str__ = commandline | |
71 | |
24 | 72 |
25 def main(args=sys.argv[1:]): | 73 def main(args=sys.argv[1:]): |
26 """CLI""" | 74 """CLI""" |
27 | 75 |
28 # available programs | 76 # available programs |
38 help="seconds to run for") | 86 help="seconds to run for") |
39 parser.add_argument("-s", "--sleep", dest="sleep", | 87 parser.add_argument("-s", "--sleep", dest="sleep", |
40 type=float, default=1., | 88 type=float, default=1., |
41 help="sleep this number of seconds between polling") | 89 help="sleep this number of seconds between polling") |
42 parser.add_argument("-p", "--prog", dest='program', | 90 parser.add_argument("-p", "--prog", dest='program', |
43 choices=progs.keys(), | 91 choices=progs.keys(), default='ping', |
44 help="subprocess to run") | 92 help="subprocess to run") |
45 # TODO parser.add_argument("--list-programs", help="list available programs") | 93 parser.add_argument("--list-programs", dest='list_programs', |
94 action='store_true', default=False, | |
95 help="list available programs") | |
46 options = parser.parse_args(args) | 96 options = parser.parse_args(args) |
47 | 97 |
48 | 98 |
49 # select program | 99 # select program |
50 prog = progs[options.program] | 100 prog = progs[options.program] |
58 while proc.poll() is None: | 108 while proc.poll() is None: |
59 curr_time = time.time() | 109 curr_time = time.time() |
60 run_time = curr_time - start | 110 run_time = curr_time - start |
61 if run_time > options.time: | 111 if run_time > options.time: |
62 proc.kill() | 112 proc.kill() |
63 if options.sleep: | |
64 time.sleep(options.sleep) | |
65 output.seek(location) | 113 output.seek(location) |
66 read = output.read() | 114 read = output.read() |
67 location += len(read) | 115 location += len(read) |
68 print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10)) | 116 print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10)) |
117 if options.sleep: | |
118 time.sleep(options.sleep) | |
69 | 119 |
70 # reset tempfile | 120 # reset tempfile |
71 output.seek(0) | 121 output.seek(0) |
72 | 122 |
73 n_lines = len(output.read().splitlines()) | 123 n_lines = len(output.read().splitlines()) |