"Subprocess.Popen": comprobación de éxito y errores
Quiero verificar si un subproceso ha finalizado la ejecución con éxito o ha fallado. Actualmente se me ocurrió una solución, pero no estoy seguro de si es correcta y confiable. ¿Está garantizado que cada proceso genera sus errores solo para stderr respetuosamentestdout
:
Nota: No estoy interesado en simplemente redirigir / imprimir la salida. Eso ya sé cómo hacerlo.
pipe = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
if "" == pipe.stdout.readline():
print("Success")
self.isCommandExectutionSuccessful = True
if not "" == pipe.stderr.readline():
print("Error")
self.isCommandExectutionSuccessful = True
alternativamente:
if "" == pipe.stdout.readline():
print("Success")
self.isCommandExectutionSuccessful = True
else:
print("Error")
self.isCommandExectutionSuccessful = False
y:
if not "" == pipe.stderr.readline():
print("Success")
self.isCommandExectutionSuccessful = True
else:
print("Error")
self.isCommandExectutionSuccessful = False