Python Forum
[subprocess] Simple way to handle error in Popen()?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[subprocess] Simple way to handle error in Popen()?
#1
Question 
Hello,

What is a simple and reliable way to handle an error when calling and monitoring a CLI application through subprocess.Popen()?

Is this good enough?
p = subprocess.Popen("blah", text=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if p.stderr:
	self.statusbar.SetStatusText("Popen error.")
	exit()
else	
	while (line := p.stdout.readline()) != "":
		self.statusbar.SetStatusText(line.strip())
output = f"End of output.  Return code: {p.wait()}"
self.statusbar.SetStatusText(output)
Thank you.
Reply
#2
This shows how to stream lines as they arrive(and avoid deadlocks) by merging stderr into stdout.
Can add other check eg timeout, exception depends on application you monitor.
import subprocess
import shlex

def stream_cmd(cmd: str, on_line, on_done):
    args = shlex.split(cmd)  # safer than shell=True
    p = subprocess.Popen(
        args,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT, # merge stderr into stdout
        text=True,
        bufsize=1,
    )

    # Stream until process exits
    for line in p.stdout:
        on_line(line.rstrip())
    rc = p.wait()
    on_done(rc)

# Example usage
if __name__ == "__main__":
    def print_line(s):
        print(s)
    def done(rc):
        print(f"== DONE, exit code {rc} ==")

    # Monitor a CLI stream
    stream_cmd("ping -n 50 127.0.0.1", print_line, done) 
So if i close the ping process before finished,it just exit with error code 1
Output:
..... Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 == DONE, exit code 1 ==
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Understanding subprocess.Popen Pedroski55 6 3,549 May-12-2024, 10:46 AM
Last Post: Pedroski55
Information subprocess.Popen() suddenly giving me grief? davecotter 3 2,586 Dec-13-2023, 10:49 PM
Last Post: davecotter
  Use subprocess.Popen and time.sleep chucky831 2 3,794 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  handle the error Irv1n 5 3,375 Nov-29-2021, 08:46 PM
Last Post: deanhystad
  Subprocess.Popen() not working when reading file path from csv file herwin 13 31,100 May-07-2021, 03:26 PM
Last Post: herwin
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 6,540 May-04-2021, 08:58 PM
Last Post: liudr
  disable subprocess.popen prompt echo paul18fr 1 3,676 Feb-04-2021, 02:50 AM
Last Post: Larz60+
  how to pass the interactive string to Popen subprocess maiya 1 3,221 Sep-18-2020, 09:36 PM
Last Post: Larz60+
  Error when running mktorrent subprocess command pythonnewbie138 4 6,780 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  How to get program output from subprocess.Popen? glestwid 1 3,757 Aug-19-2020, 05:44 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020