annotate python/multiproc.py @ 700:2a3323626bcf

wfm
author Jeff Hammel <k0scist@gmail.com>
date Wed, 13 Aug 2014 11:24:09 -0700
parents 3da0a7caf07e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 multiprocessing/subprocess experiments
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import argparse
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import os
617
3da0a7caf07e comment
Jeff Hammel <k0scist@gmail.com>
parents: 616
diff changeset
9 import subprocess # http://bugs.python.org/issue1731717
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import sys
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import time
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import tempfile
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
14 string = (str, unicode)
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
15
601
7f189613a289 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 600
diff changeset
16 class Process(subprocess.Popen):
7f189613a289 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 600
diff changeset
17 """why would you name a subprocess object Popen?"""
7f189613a289 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 600
diff changeset
18
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
19 # http://docs.python.org/2/library/subprocess.html#popen-constructor
610
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
20 defaults = {'bufsize': 1, # line buffered
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
21 'store_output': True, # store stdout
603
bd30deecdf4a STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 602
diff changeset
22 }
bd30deecdf4a STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 602
diff changeset
23
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
24 def __init__(self, command, **kwargs):
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
25
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
26 # setup arguments
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
27 self.command = command
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
28 _kwargs = self.defaults.copy()
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
29 _kwargs.update(kwargs)
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
30
607
567492ef7b57 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 606
diff changeset
31 # on unix, ``shell={True|False}`` should always come from the
567492ef7b57 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 606
diff changeset
32 # type of command (string or list)
609
3ea759399b8f STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 608
diff changeset
33 if not subprocess.mswindows:
607
567492ef7b57 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 606
diff changeset
34 _kwargs['shell'] = isinstance(command, string)
567492ef7b57 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 606
diff changeset
35
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
36 # output buffer
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
37 self.location = 0
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
38 self.output_buffer = tempfile.SpooledTemporaryFile()
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
39 self.output = '' if _kwargs.pop('store_output') else None
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
40 _kwargs['stdout'] = self.output_buffer
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
41
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
42 # runtime
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
43 self.start = time.time()
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
44 self.end = None
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
45
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
46 # launch subprocess
607
567492ef7b57 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 606
diff changeset
47 subprocess.Popen.__init__(self, command, **_kwargs)
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
48
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
49 def _finalize(self, process_output):
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
50 """internal function to finalize"""
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
51
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
52 # read final output
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
53 self.read(process_output)
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
54
614
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
55 # reset output buffer location
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
56 self.output_buffer.seek(0)
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
57
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
58 # set end time
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
59 self.end = time.time()
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
60
614
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
61 def poll(self, process_output=None):
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
62
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
63 if process_output is not None:
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
64 self.read(process_output) # read from output buffer
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
65 poll = subprocess.Popen.poll(self)
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
66 if poll is not None:
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
67 self._finalize(process_output)
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
68 return poll
613
bf3f8bc6d1b3 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 612
diff changeset
69
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
70 def wait(self, maxtime=None, sleep=1., process_output=None):
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
71 """
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
72 maxtime -- timeout in seconds
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
73 sleep -- number of seconds to sleep between polling
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
74 """
614
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
75 while self.poll(process_output) is None:
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
76
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
77 # check for timeout
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
78 curr_time = time.time()
613
bf3f8bc6d1b3 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 612
diff changeset
79 run_time = self.runtime()
612
839dfd35d567 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 611
diff changeset
80 if maxtime is not None and run_time > maxtime:
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
81 self.kill()
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
82 self._finalize(process_output)
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
83 return
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
84
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
85 # naptime
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
86 if sleep:
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
87 time.sleep(sleep)
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
88
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
89 # finalize
613
bf3f8bc6d1b3 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 612
diff changeset
90 self._finalize(process_output)
608
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
91
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
92 return self.returncode # set by ``.poll()``
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
93
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
94 def read(self, process_output=None):
610
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
95 """read from the output buffer"""
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
96
610
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
97 self.output_buffer.seek(self.location)
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
98 read = self.output_buffer.read()
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
99 if self.output is not None:
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
100 self.output += read
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
101 if process_output:
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
102 process_output(read)
610
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
103 self.location += len(read)
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
104 return read
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
105
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
106 def commandline(self):
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
107 """returns string of command line"""
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
108
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
109 if isinstance(self.command, string):
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
110 return self.command
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
111 return subprocess.list2cmdline(self.command)
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
112
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
113 __str__ = commandline
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
114
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
115 def runtime(self):
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
116 """returns time spent running or total runtime if completed"""
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
117
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
118 if self.end is None:
613
bf3f8bc6d1b3 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 612
diff changeset
119 return time.time() - self.start
bf3f8bc6d1b3 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 612
diff changeset
120 return self.end - self.start
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
121
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
122
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
123 def main(args=sys.argv[1:]):
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
124 """CLI"""
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
125
615
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
126 description = """demonstration of how to do things with subprocess"""
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
127
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
128 # available programs
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
129 progs = {'yes': ["yes"],
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
130 'ping': ['ping', 'google.com']}
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
131
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
132 # parse command line
615
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
133 parser = argparse.ArgumentParser(description=description)
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
134 parser.add_argument("-t", "--time", dest="time",
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
135 type=float, default=4.,
614
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
136 help="seconds to run for (or <= 0 for forever)")
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
137 parser.add_argument("-s", "--sleep", dest="sleep",
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
138 type=float, default=1.,
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
139 help="sleep this number of seconds between polling")
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
140 parser.add_argument("-p", "--prog", dest='program',
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
141 choices=progs.keys(), default='ping',
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
142 help="subprocess to run")
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
143 parser.add_argument("--list-programs", dest='list_programs',
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
144 action='store_true', default=False,
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
145 help="list available programs")
615
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
146 parser.add_argument("--wait", dest='wait',
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
147 action='store_true', default=False,
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
148 help="run with ``.wait()`` and a callback")
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
149 parser.add_argument("--callback", dest='callback',
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
150 action='store_true', default=False,
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
151 help="run with polling and a callback")
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
152 options = parser.parse_args(args)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
153
608
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
154 # list programs
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
155 if options.list_programs:
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
156 for key in sorted(progs.keys()):
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
157 print ('{}: {}'.format(key, subprocess.list2cmdline(progs[key])))
610
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
158 sys.exit(0)
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
159
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
160 # select program
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
161 prog = progs[options.program]
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
162
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
163 # start process
608
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
164 proc = Process(prog)
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
165
615
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
166 # demo function for processing output
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
167 def output_processor(output):
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
168 print ('[{}]:\n{}\n{}'.format(proc.runtime(),
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
169 output.upper(),
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
170 '-==-'*10))
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
171 if options.callback:
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
172 process_output = output_processor
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
173 else:
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
174 process_output = None
613
bf3f8bc6d1b3 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 612
diff changeset
175
615
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
176 if options.wait:
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
177 # wait for being done
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
178 proc.wait(maxtime=options.time, sleep=options.sleep, process_output=output_processor)
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
179 else:
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
180 # start the main subprocess loop
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
181 while proc.poll(process_output) is None:
608
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
182
615
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
183 if options.time > 0 and proc.runtime() > options.time:
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
184 proc.kill()
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
185
615
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
186 if options.sleep:
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
187 time.sleep(options.sleep)
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
188
616
41133baea90d STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 615
diff changeset
189 if process_output is None:
615
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
190 # process the output with ``.read()`` call
616
41133baea90d STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 615
diff changeset
191 read = proc.read()
41133baea90d STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 615
diff changeset
192 output_processor(read)
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
193
614
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
194 # correctness tests
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
195 assert proc.end is not None
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
196
f905378fcee0 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 613
diff changeset
197 # print summary
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
198 output = proc.output
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
199 n_lines = len(output.splitlines())
615
6db42d965f0e STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 614
diff changeset
200 print ("{}: {} lines, ran for {} seconds".format(subprocess.list2cmdline(prog), n_lines, proc.runtime()))
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
201
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
202 if __name__ == '__main__':
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
203 main()