annotate python/multiproc.py @ 613:bf3f8bc6d1b3

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