comparison python/multiproc.py @ 608:9a5cdd49af53

STUB: python/multiproc.py
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Feb 2014 11:37:34 -0800
parents 567492ef7b57
children 3ea759399b8f
comparison
equal deleted inserted replaced
607:567492ef7b57 608:9a5cdd49af53
63 63
64 # naptime 64 # naptime
65 if sleep: 65 if sleep:
66 time.sleep(sleep) 66 time.sleep(sleep)
67 67
68 # reset tempfile
69 output.seek(0)
70
71 return self.returncode # set by ``.poll()``
68 72
69 def commandline(self): 73 def commandline(self):
70 """returns string of command line""" 74 """returns string of command line"""
71 75
72 if isinstance(self.command, string): 76 if isinstance(self.command, string):
99 parser.add_argument("--list-programs", dest='list_programs', 103 parser.add_argument("--list-programs", dest='list_programs',
100 action='store_true', default=False, 104 action='store_true', default=False,
101 help="list available programs") 105 help="list available programs")
102 options = parser.parse_args(args) 106 options = parser.parse_args(args)
103 107
108 # list programs
109 if options.list_programs:
110 for key in sorted(progs.keys()):
111 print ('{}: {}'.format(key, subprocess.list2cmdline(progs[key])))
104 112
105 # select program 113 # select program
106 prog = progs[options.program] 114 prog = progs[options.program]
107 115
108 # start the main subprocess loop 116 proc = Process(prog)
109 # TODO -> OO
110 output = tempfile.SpooledTemporaryFile()
111 start = time.time()
112 proc = subprocess.Popen(prog, stdout=output)
113 location = 0
114 while proc.poll() is None:
115 curr_time = time.time()
116 run_time = curr_time - start
117 if run_time > options.time:
118 proc.kill()
119 output.seek(location)
120 read = output.read()
121 location += len(read)
122 print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10))
123 if options.sleep:
124 time.sleep(options.sleep)
125 117
126 # reset tempfile 118 # # start the main subprocess loop
127 output.seek(0) 119 # # TODO -> OO
120 # output = tempfile.SpooledTemporaryFile()
121 # start = time.time()
122 # proc = subprocess.Popen(prog, stdout=output)
123 # location = 0
124 # while proc.poll() is None:
125 # curr_time = time.time()
126 # run_time = curr_time - start
127 # if run_time > options.time:
128 # proc.kill()
129 # output.seek(location)
130 # read = output.read()
131 # location += len(read)
132 # print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10))
133 # if options.sleep:
134 # time.sleep(options.sleep)
135
136 # # reset tempfile
137 # output.seek(0)
128 138
129 n_lines = len(output.read().splitlines()) 139 n_lines = len(output.read().splitlines())
130 print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines)) 140 print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines))
131 141
132 if __name__ == '__main__': 142 if __name__ == '__main__':