comparison 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
comparison
equal deleted inserted replaced
612:839dfd35d567 613:bf3f8bc6d1b3
56 self.output_buffer.seek(0) 56 self.output_buffer.seek(0)
57 57
58 # set end time 58 # set end time
59 self.end = time.time() 59 self.end = time.time()
60 60
61 def poll(self):
62 return subprocess.Popen.poll(self)
63
61 def wait(self, maxtime=None, sleep=1., process_output=None): 64 def wait(self, maxtime=None, sleep=1., process_output=None):
62 """ 65 """
63 maxtime -- timeout in seconds 66 maxtime -- timeout in seconds
64 sleep -- number of seconds to sleep between polling 67 sleep -- number of seconds to sleep between polling
65 """ 68 """
66 while self.poll() is None: 69 while self.poll() is None:
67 70
68 # check for timeout 71 # check for timeout
69 curr_time = time.time() 72 curr_time = time.time()
70 run_time = curr_time - self.start 73 run_time = self.runtime()
71 if maxtime is not None and run_time > maxtime: 74 if maxtime is not None and run_time > maxtime:
72 self.kill() 75 self.kill()
73 self._finalize(process_output) 76 self._finalize(process_output)
74 return 77 return
75 78
79 # naptime 82 # naptime
80 if sleep: 83 if sleep:
81 time.sleep(sleep) 84 time.sleep(sleep)
82 85
83 # finalize 86 # finalize
84 self._finalize() 87 self._finalize(process_output)
85 88
86 return self.returncode # set by ``.poll()`` 89 return self.returncode # set by ``.poll()``
87 90
88 def read(self, process_output=None): 91 def read(self, process_output=None):
89 """read from the output buffer""" 92 """read from the output buffer"""
108 111
109 def runtime(self): 112 def runtime(self):
110 """returns time spent running or total runtime if completed""" 113 """returns time spent running or total runtime if completed"""
111 114
112 if self.end is None: 115 if self.end is None:
113 return self.end - self.start 116 return time.time() - self.start
114 return time.time() - self.start 117 return self.end - self.start
115 118
116 119
117 def main(args=sys.argv[1:]): 120 def main(args=sys.argv[1:]):
118 """CLI""" 121 """CLI"""
119 122
149 # start process 152 # start process
150 proc = Process(prog) 153 proc = Process(prog)
151 154
152 # callback for output processing 155 # callback for output processing
153 def process_output(output): 156 def process_output(output):
154 print output.upper() 157 print ('[{}] {}\n{}'.format(proc.runtime(),
158 output.upper(),
159 '-==-'*10))
155 160
156 # # start the main subprocess loop 161
157 # # TODO -> OO 162 # LEGACY: output = tempfile.SpooledTemporaryFile()
158 # output = tempfile.SpooledTemporaryFile()
159 # start = time.time() 163 # start = time.time()
160 # proc = subprocess.Popen(prog, stdout=output) 164 # proc = subprocess.Popen(prog, stdout=output)
161 # location = 0 165 # location = 0
162 # while proc.poll() is None: 166
163 # curr_time = time.time() 167 # start the main subprocess loop
164 # run_time = curr_time - start 168 # while proc.poll() is None:
165 # if run_time > options.time: 169
166 # proc.kill() 170 # LEGACY:
171 # curr_time = time.time()
172 # run_time = curr_time - start
173 # if run_time > options.time:
174 # proc.kill()
175
176
167 # output.seek(location) 177 # output.seek(location)
168 # read = output.read() 178 # read = output.read()
169 # location += len(read) 179 # location += len(read)
170 # print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10)) 180 # print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10))
171 # if options.sleep: 181 # if options.sleep: