Jan-20-2022, 04:17 AM
(Jan-18-2022, 05:26 AM)bowlofred Wrote: You're not showing how you're calling subprocess. subprocess.run is the easy way, but waits for the process to exit. If you want to read from stdout before it exits, you'll have to use subprocess.Popen directly.
If I change your program above to have it print with flush=True and call it "sender.py", then the following program will display the progress. You can do what you need with the data.
Note that this one should work okay because you're constantly getting output and you shouldn't need to do anything between the progress updates. The readline() is going to block until you get more data. If you had to do work while waiting for output, you'd need a different method.
import subprocess cmd = ["python3", "sender.py"] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, text=True) while (line := p.stdout.readline()) != "": line = line.rstrip() if '"Progress"' in line: progress = float(line.split(":")[1].rstrip(",")) print(f"Progress: {progress:.2f}") print(f"End of output. Return code: {p.wait()}")
Output:Progress: 0.00 Progress: 20.00 Progress: 40.00 Progress: 60.00 Progress: 80.00 Progress: 100.00 End of output. Return code: 0
That will work perfectly! Thank you for the help!
I hadn't responded for a while since I'm a bit of an issue with my Python interpreters, but I'll fix that later.