annotate python/example/process.py @ 923:b25fbb9257ec

Breeze GTK theme
author Jeff Hammel <k0scist@gmail.com>
date Thu, 14 Nov 2024 09:30:28 -0800
parents 5364185dcb9b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
735
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
1 #!/usr/bin/env python
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
2
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
3 """
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
4 multiprocessing/subprocess front-end
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
5 """
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
6
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
7 # imports
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
8 import argparse
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
9 import atexit
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import os
735
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
11 import signal
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
12 import subprocess # http://bugs.python.org/issue1731717
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 import sys
735
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
14 import time
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
15 import tempfile
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
16
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
17
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
18 # globals
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
19 __all__ = ['Process']
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
20 string = (str, unicode)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
21 PIDS = set()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
22
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
23 # ensure subprocesses gets killed on exit
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
24 def killall():
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
25 """kill all subprocess PIDs"""
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
26 global PIDS
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
27 for pid in PIDS.copy():
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
28 try:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
29 os.kill(pid, 9) # SIGKILL
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
30 PIDS.discard(pid)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
31 except:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
32 sys.stderr.write("Unable to kill PID {}\n".format(pid))
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
33 atexit.register(killall)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
34
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
35
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
36 signals = (signal.SIGHUP, signal.SIGINT, signal.SIGQUIT, signal.SIGSEGV, signal.SIGTERM) # signals to handle
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
37 fatal = set([signal.SIGINT, signal.SIGSEGV, signal.SIGKILL, signal.SIGTERM])
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
38 # ensure subprocesses get killed on signals
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
39 def sighandler(signum, frame):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
40 """https://docs.python.org/2/library/signal.html"""
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
41 sys.stderr.write('Signal handler called with signal {}\n; terminating subprocesses: {}'.format(signum,
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
42 ', '.join([str(pid) for pid in sorted(PIDS)])))
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
43 killall()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
44 if signum in fatal:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
45 print ("Caught signal {}; exiting".format(signum))
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
46 sys.exit()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
47 for signum in signals:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
48 try:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
49 signal.signal(signum, sighandler)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
50 except RuntimeError as e:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
51 print ('[{}] {}'.format(signum, e))
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
52 raise
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
53
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
54 class Process(subprocess.Popen):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
55 """why would you name a subprocess object Popen?"""
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
56
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
57 # http://docs.python.org/2/library/subprocess.html#popen-constructor
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
58 defaults = {'bufsize': 1, # line buffered
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
59 'store_output': True, # store stdout
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
60 }
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
61
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
62 def __init__(self, command, **kwargs):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
63
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
64 # get verbosity
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
65 self.verbose = kwargs.pop('verbose', False)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
66
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
67 # setup arguments
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
68 self.command = command
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
69 _kwargs = self.defaults.copy()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
70 _kwargs.update(kwargs)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
71
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
72
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
73 # on unix, ``shell={True|False}`` should always come from the
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
74 # type of command (string or list)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
75 if not subprocess.mswindows:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
76 _kwargs['shell'] = isinstance(command, string)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
77
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
78 # output buffer
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
79 self.location = 0
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
80 self.output_buffer = tempfile.SpooledTemporaryFile()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
81 self.output = '' if _kwargs.pop('store_output') else None
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
82 _kwargs['stdout'] = self.output_buffer
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
83
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
84 # ensure child in process group
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
85 # see :
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
86 # - http://pymotw.com/2/subprocess/#process-groups-sessions
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
87 # - http://ptspts.blogspot.com/2012/11/how-to-start-and-kill-unix-process-tree.html
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
88 _kwargs['preexec_fn'] = os.setpgrp
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
89
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
90 # runtime
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
91 self.start = time.time()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
92 self.end = None
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
93
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
94 if self.verbose:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
95 # print useful info
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
96 print ("Running `{}`; started: {}".format(str(self), self.start))
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
97
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
98 # launch subprocess
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
99 try:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
100 subprocess.Popen.__init__(self, command, **_kwargs)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
101 PIDS.add(self.pid)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
102 if self.verbose:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
103 # print the PID
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
104 print ("PID: {}".format(self.pid))
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
105 except:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
106 # print the command
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
107 print ("Failure to run:")
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
108 print (self.command)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
109
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
110 # reraise the hard way:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
111 # http://www.ianbicking.org/blog/2007/09/re-raising-exceptions.html
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
112 exc = sys.exc_info()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
113 raise exc[0], exc[1], exc[2]
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
114
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
115
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
116 def _finalize(self, process_output):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
117 """internal function to finalize"""
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
118
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
119 # read final output
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
120 if process_output is not None:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
121 self.read(process_output)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
122
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
123 # reset output buffer location
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
124 self.output_buffer.seek(0)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
125
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
126 # set end time
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
127 self.end = time.time()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
128
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
129 # remove PID from list
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
130 PIDS.discard(self.pid)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
131
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
132 def poll(self, process_output=None):
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
133
735
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
134 if process_output is not None:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
135 self.read(process_output) # read from output buffer
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
136 poll = subprocess.Popen.poll(self)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
137 if poll is not None:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
138 self._finalize(process_output)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
139 return poll
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
140
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
141 def wait(self, maxtime=None, sleep=1., process_output=None):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
142 """
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
143 maxtime -- timeout in seconds
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
144 sleep -- number of seconds to sleep between polling
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
145 """
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
146 while self.poll(process_output) is None:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
147
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
148 # check for timeout
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
149 curr_time = time.time()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
150 run_time = self.runtime()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
151 if maxtime is not None and run_time > maxtime:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
152 self.kill()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
153 self._finalize(process_output)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
154 return
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
155
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
156 # naptime
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
157 if sleep:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
158 time.sleep(sleep)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
159
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
160 # finalize
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
161 self._finalize(process_output)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
162
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
163 return self.returncode # set by ``.poll()``
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
164
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
165 def read(self, process_output=None):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
166 """read from the output buffer"""
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
167
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
168 self.output_buffer.seek(self.location)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
169 read = self.output_buffer.read()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
170 if self.output is not None:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
171 self.output += read
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
172 if process_output:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
173 process_output(read)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
174 self.location += len(read)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
175 return read
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
176
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
177 def commandline(self):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
178 """returns string of command line"""
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
179
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
180 if isinstance(self.command, string):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
181 return self.command
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
182 return subprocess.list2cmdline(self.command)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
183
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
184 __str__ = commandline
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
185
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
186 def runtime(self):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
187 """returns time spent running or total runtime if completed"""
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
188
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
189 if self.end is None:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
190 return time.time() - self.start
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
191 return self.end - self.start
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
192
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
193
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
194 def main(args=sys.argv[1:]):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
195 """CLI"""
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
196
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
197 description = """demonstration of how to do things with subprocess"""
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
198
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
199 # available programs
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
200 progs = {'yes': ["yes"],
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
201 'ping': ['ping', 'google.com']}
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
202
735
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
203 # parse command line
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
204 parser = argparse.ArgumentParser(description=description)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
205 parser.add_argument("-t", "--time", dest="time",
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
206 type=float, default=4.,
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
207 help="seconds to run for (or <= 0 for forever)")
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
208 parser.add_argument("-s", "--sleep", dest="sleep",
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
209 type=float, default=1.,
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
210 help="sleep this number of seconds between polling")
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
211 parser.add_argument("-p", "--prog", dest='program',
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
212 choices=progs.keys(), default='ping',
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
213 help="subprocess to run")
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
214 parser.add_argument("--list-programs", dest='list_programs',
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
215 action='store_true', default=False,
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
216 help="list available programs")
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
217 parser.add_argument("--wait", dest='wait',
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
218 action='store_true', default=False,
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
219 help="run with ``.wait()`` and a callback")
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
220 parser.add_argument("--callback", dest='callback',
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
221 action='store_true', default=False,
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
222 help="run with polling and a callback")
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
223 options = parser.parse_args(args)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
224
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
225 # list programs
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
226 if options.list_programs:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
227 for key in sorted(progs.keys()):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
228 print ('{}: {}'.format(key, subprocess.list2cmdline(progs[key])))
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
229 sys.exit(0)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
230
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
231 # select program
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
232 prog = progs[options.program]
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
233
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
234 # start process
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
235 proc = Process(prog)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
236
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
237 # demo function for processing output
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
238 def output_processor(output):
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
239 print ('[{}]:\n{}\n{}'.format(proc.runtime(),
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
240 output.upper(),
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
241 '-==-'*10))
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
242 if options.callback:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
243 process_output = output_processor
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
244 else:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
245 process_output = None
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
246
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
247 if options.wait:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
248 # wait for being done
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
249 proc.wait(maxtime=options.time, sleep=options.sleep, process_output=output_processor)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
250 else:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
251 # start the main subprocess loop
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
252 while proc.poll(process_output) is None:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
253
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
254 if options.time > 0 and proc.runtime() > options.time:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
255 proc.kill()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
256
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
257 if options.sleep:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
258 time.sleep(options.sleep)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
259
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
260 if process_output is None:
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
261 # process the output with ``.read()`` call
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
262 read = proc.read()
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
263 output_processor(read)
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
264
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
265 # correctness tests
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
266 assert proc.end is not None
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
267
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
268 # print summary
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
269 output = proc.output
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
270 n_lines = len(output.splitlines())
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
271 print ("{}: {} lines, ran for {} seconds".format(subprocess.list2cmdline(prog), n_lines, proc.runtime()))
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
272
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
273 if __name__ == '__main__':
735
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
274 main()