I have a python program that starts subprocesses using Popenand consumes their output in almost real time as it is created. The code for the corresponding cycle:
def run(self, output_consumer):
self.prepare_to_run()
popen_args = self.get_popen_args()
logging.debug("Calling popen with arguments %s" % popen_args)
self.popen = subprocess.Popen(**popen_args)
while True:
outdata = self.popen.stdout.readline()
if not outdata and self.popen.returncode is not None:
break
output_consumer.process_output(outdata)
self.popen.poll()
output_consumer.finish(self.popen.returncode)
self.post_run()
def get_popen_args(self):
return {
'args': self.command,
'shell': False,
'bufsize': 0,
'cwd': self.get_cwd(),
'env': self.get_environment(),
'stdout': subprocess.PIPE,
'stderr': subprocess.STDOUT,
'close_fds': True,
}
This works fine on my production machines, but on my dev machine, the call .readline()hangs when some subprocesses finish. That is, it will successfully process all the output, including the final output line, saying "end of process", but then it will poll again readlineand never return. This method correctly exits to the dev machine for most of the subprocesses that I invoke, but cannot sequentially exit for one complex bash script that itself invokes many subprocesses.
, popen.returncode None ( 0) . , , , , . , , , , readline() . read() . read(1) , . popen.stdout.closed False. , ?
python 2.7.3 Ubuntu 12.04LTS. FWIW, stderr stdout stderr=subprocess.STDOUT.
? - stdout? - - , - ? , , dev-, supervisord? , , , ?