I've written these 2 methods and I'm wondering if the 2nd method, which uses yield is more efficient or if I'm using yield wrong. Can someone refactor the 2nd method to make it more efficient for reading system os calls with large output?
def exec_cmd(command):
proc = subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = proc.communicate()
return_code = proc.returncode
if stderr:
raise Error(stdout, stderr)
if return_code:
raise Error(stdout, return_code)
return stdout, return_code
def exec_cmd_output_line_by_line(command):
proc = subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return_code = proc.returncode
if return_code:
raise Error(proc.stdout, return_code)
for line in proc.stdout.readlines():
yield line.strip()