annotate python/multiproc.py @ 611:8e23bbc9c197

STUB: python/multiproc.py
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Feb 2014 15:32:29 -0800
parents 5ce25399da67
children 839dfd35d567
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
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import subprocess
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
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
55 # reset output buffer
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
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
61 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
62 """
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
63 maxtime -- timeout in seconds
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
64 sleep -- number of seconds to sleep between polling
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
65 """
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
66 while self.poll() is None:
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
67
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
68 # check for timeout
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
69 curr_time = time.time()
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
70 run_time = curr_time - self.start
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
71 if run_time > maxtime:
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
72 self.kill()
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
73 self._finalize(process_output)
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
74 return
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
75
607
567492ef7b57 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 606
diff changeset
76 # read from output buffer
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
77 self.read(process_output)
607
567492ef7b57 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 606
diff changeset
78
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
79 # naptime
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
80 if sleep:
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
81 time.sleep(sleep)
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
82
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
83 # finalize
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
84 self._finalize()
608
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
85
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
86 return self.returncode # set by ``.poll()``
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
87
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
88 def read(self, process_output=None):
610
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
89 """read from the output buffer"""
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
90
610
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
91 self.output_buffer.seek(self.location)
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
92 read = self.output_buffer.read()
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
93 if self.output is not None:
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
94 self.output += read
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
95 if process_output:
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
96 process_output(read)
610
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
97 self.location += len(read)
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
98 return read
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
99
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
100 def commandline(self):
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
101 """returns string of command line"""
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
102
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
103 if isinstance(self.command, string):
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
104 return self.command
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
105 return subprocess.list2cmdline(self.command)
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
106
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
107 __str__ = commandline
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
108
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
109 def runtime(self):
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
110 """returns time spent running or total runtime if completed"""
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
111
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
112 if self.end is None:
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
113 return self.end - self.start
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
114 return time.time() - self.start
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
115
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
116
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
117 def main(args=sys.argv[1:]):
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
118 """CLI"""
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
119
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
120 # available programs
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
121 progs = {'yes': ["yes"],
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
122 'ping': ['ping', 'google.com']}
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
123
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
124 # parse command line
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
125 parser = argparse.ArgumentParser(description=__doc__)
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
126 parser.add_argument("-t", "--time", dest="time",
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
127 type=float, default=4.,
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
128 help="seconds to run for")
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
129 parser.add_argument("-s", "--sleep", dest="sleep",
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
130 type=float, default=1.,
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
131 help="sleep this number of seconds between polling")
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
132 parser.add_argument("-p", "--prog", dest='program',
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
133 choices=progs.keys(), default='ping',
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
134 help="subprocess to run")
606
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
135 parser.add_argument("--list-programs", dest='list_programs',
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
136 action='store_true', default=False,
5b561c7b2005 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 603
diff changeset
137 help="list available programs")
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
138 options = parser.parse_args(args)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
139
608
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
140 # list programs
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
141 if options.list_programs:
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
142 for key in sorted(progs.keys()):
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
143 print ('{}: {}'.format(key, subprocess.list2cmdline(progs[key])))
610
5ce25399da67 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 609
diff changeset
144 sys.exit(0)
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
145
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
146 # select program
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
147 prog = progs[options.program]
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
148
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
149 # start process
608
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
150 proc = Process(prog)
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
151
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
152 # callback for output processing
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
153 def process_output(output):
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
154 print output.upper()
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
155
608
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
156 # # start the main subprocess loop
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
157 # # TODO -> OO
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
158 # output = tempfile.SpooledTemporaryFile()
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
159 # start = time.time()
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
160 # proc = subprocess.Popen(prog, stdout=output)
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
161 # location = 0
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
162 # while proc.poll() is None:
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
163 # curr_time = time.time()
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
164 # run_time = curr_time - start
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
165 # if run_time > options.time:
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
166 # proc.kill()
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
167 # output.seek(location)
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
168 # read = output.read()
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
169 # location += len(read)
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
170 # print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10))
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
171 # if options.sleep:
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
172 # time.sleep(options.sleep)
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
173
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
174 # # reset tempfile
9a5cdd49af53 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 607
diff changeset
175 # output.seek(0)
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
176
611
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
177 # wait for being done
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
178 proc.wait(maxtime=options.time, sleep=options.sleep, process_output=process_output)
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
179
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
180 # finalization
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
181 output = proc.output
8e23bbc9c197 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 610
diff changeset
182 n_lines = len(output.splitlines())
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
183 print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines))
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
184
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
185 if __name__ == '__main__':
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
186 main()