comparison python/multiproc.py @ 610:5ce25399da67

STUB: python/multiproc.py
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Feb 2014 12:40:07 -0800
parents 3ea759399b8f
children 8e23bbc9c197
comparison
equal deleted inserted replaced
609:3ea759399b8f 610:5ce25399da67
15 15
16 class Process(subprocess.Popen): 16 class Process(subprocess.Popen):
17 """why would you name a subprocess object Popen?""" 17 """why would you name a subprocess object Popen?"""
18 18
19 # http://docs.python.org/2/library/subprocess.html#popen-constructor 19 # http://docs.python.org/2/library/subprocess.html#popen-constructor
20 defaults = {'buffsize': 1, # line buffered 20 defaults = {'bufsize': 1, # line buffered
21 } 21 }
22 22
23 def __init__(self, command, **kwargs): 23 def __init__(self, command, **kwargs):
24 24
25 # setup arguments 25 # setup arguments
33 _kwargs['shell'] = isinstance(command, string) 33 _kwargs['shell'] = isinstance(command, string)
34 34
35 # output buffer 35 # output buffer
36 self.output_buffer = tempfile.SpooledTemporaryFile() 36 self.output_buffer = tempfile.SpooledTemporaryFile()
37 self.location = 0 37 self.location = 0
38 self.output = [] 38 self.output = ''
39 _kwargs['stdout'] = self.output_buffer 39 _kwargs['stdout'] = self.output_buffer
40 40
41 # launch subprocess 41 # launch subprocess
42 self.start = time.time() 42 self.start = time.time()
43 subprocess.Popen.__init__(self, command, **_kwargs) 43 subprocess.Popen.__init__(self, command, **_kwargs)
55 if run_time > maxtime: 55 if run_time > maxtime:
56 # TODO: read from output 56 # TODO: read from output
57 return process.kill() 57 return process.kill()
58 58
59 # read from output buffer 59 # read from output buffer
60 self.output_buffer.seek(self.location) 60 read = self.read()
61 read = self.output_buffer.read()
62 self.location += len(read)
63 61
64 # naptime 62 # naptime
65 if sleep: 63 if sleep:
66 time.sleep(sleep) 64 time.sleep(sleep)
67 65
68 # reset tempfile 66 # reset tempfile
69 output.seek(0) 67 output.seek(0)
70 68
71 return self.returncode # set by ``.poll()`` 69 return self.returncode # set by ``.poll()``
70
71 def read(self):
72 """read from the output buffer"""
73 self.output_buffer.seek(self.location)
74 read = self.output_buffer.read()
75 self.output += read
76 self.location += len(read)
77 return read
72 78
73 def commandline(self): 79 def commandline(self):
74 """returns string of command line""" 80 """returns string of command line"""
75 81
76 if isinstance(self.command, string): 82 if isinstance(self.command, string):
107 113
108 # list programs 114 # list programs
109 if options.list_programs: 115 if options.list_programs:
110 for key in sorted(progs.keys()): 116 for key in sorted(progs.keys()):
111 print ('{}: {}'.format(key, subprocess.list2cmdline(progs[key]))) 117 print ('{}: {}'.format(key, subprocess.list2cmdline(progs[key])))
118 sys.exit(0)
112 119
113 # select program 120 # select program
114 prog = progs[options.program] 121 prog = progs[options.program]
115 122
116 proc = Process(prog) 123 proc = Process(prog)