http://stackoverflow.com/questions/1180606/using-subprocess-popen-for-process-with-large-output
http://stackoverflow.com/questions/16768290/working-of-popen-communicate
# It takes care of standard buffer overflow:
# In not needed: stdout or stderror may be set as: None
# By default, all arguments are initialized to None except bufsize=0 and required first argument 'args'
# PIPE => data go to system buffer
# PIPE is needed to redirect the output to a variable
# stdout and stderror could be: None, PIPE, or a file handle
# Note: Popen command (cmd) must be a list
from subprocess import Popen, PIPE, STDOUTclass BaseScript(object):
@staticmethod
def runCmd(cmdName):
p = subprocess.Popen(cmd , shell=True, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
return out.rstrip(), err.rstrip(), p.returncode
# stdout = <file_handle>
f=file('data.out','w')
p = subprocess.Popen(cmd, shell=True, stdout=f, stderr=subprocess.PIPE)
errcode = p.wait()
f.close()
if errcode:
errmess = p.stderr.read()
log.error('cmd failed <%s>: %s' % (errcode,errmess))
for line in file('data.out'):
#do something
No comments:
Post a Comment