|
|
|
|
|
by theamk
767 days ago
|
|
It looks like you used some old tutorials? "subprocess.run" appeared in python 3.5, and it's pretty nice - for example you so "check=True" to raise on error exit code, and omit it if you want to check exit code yourself. And to get text output you put "text=True" (or encoding="utf-8" if you are unsure what the system encoding is) As for your boilerplate, it seems "p.stdout.splitlines()" is what you want? it's what you normally want to use to parse process output line-by-line The background process is the hardest part, but for the most common case, you don't need any thread: proc = subprocess.Popen(["slow-app", "arg"], stdout=subprocess.PIPE, text=True)
for line in proc.stdout:
print("slow-app said:", line.rstrip())
print("slow-app finished, exit code", proc.wait())
sadly if you need to parse multiple streams, threads are often the easiest. |
|