Skip to content

Instantly share code, notes, and snippets.

@shcallaway
Last active September 24, 2018 19:15
Show Gist options
  • Save shcallaway/3fb25839838bf044ba0555ffbb748fca to your computer and use it in GitHub Desktop.
Save shcallaway/3fb25839838bf044ba0555ffbb748fca to your computer and use it in GitHub Desktop.
Stream stdout from a Python subprocess
#!/bin/bash
for i in `seq 1 10`; do
echo $i
if [ "$i" = "5" ]; then
>&2 echo "Error!"
exit 1
fi
sleep 1
done
import subprocess
def chomp(string):
while string.endswith("\n"):
string = string[:-1]
return string
if __name__ == '__main__':
command = "./counter.sh"
print(f"Command: {command}")
process = subprocess.Popen(command,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
for stdout in iter(process.stdout.readline, b''):
stdout = chomp(stdout.decode())
print(stdout)
# Unfortunately this solution does not merge stdout and stderr streams
for stderr in iter(process.stderr.readline, b''):
stderr = chomp(stderr.decode())
print(stderr)
# This handles a rare case where stdout and stderr are finished streaming but
# the process is not complete and the returncode property has not been set
process.wait()
# Throw exception if process returns a non-zero exit code
if process.returncode is not 0:
raise Exception(
f"'{command}' exited with non-zero code: {process.returncode}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment