Take this code as an example (tar can compress by -z -J -j and there is a tarfile specific module, i know, but it's to represent a long running process)
from subprocess import Popen, PIPE
with open('tarball.tar.gz', 'w+') as tarball:
tarcmd = Popen(['tar', '-cvf', '-', '/home'], stdout=PIPE)
zipcmd = Popen(['gzip', '-c'], stdin=tarcmd.stdout, stdout=tarball)
tarcmd.stdout.close()
zipcmd.communicate()
# added a while loop that breaks when tarcmd gets a
# proper return value. Can it be considerate a good
# solution?
while tarcmd.poll() is None:
print('waiting...')
# test the values and do stuff accordingly
This is the typical example of piping two commands in python subprocess. Now checking the return code of the zipcmd is easy, but how to check if tarcmd fails? if i check its returncode i always get none (i think because stdout it's closed). Basically i wanna raise an exception if one of the two command fails. In bash there is $PIPESTATUS, how can i do it in python?