Python Forum
How to parse a live feed in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to parse a live feed in Python?
#3
(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.
Reply


Messages In This Thread
How to parse a live feed in Python? - by Daring_T - Jan-18-2022, 04:07 AM
RE: How to parse a live feed in Python? - by Daring_T - Jan-20-2022, 04:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 208 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Draw bounding boxes on live feed Jerome 0 301 Jan-20-2024, 10:50 PM
Last Post: Jerome
  Is it possible to capture live running waveforms in an oscilloscope using python? Girishbabu_27 4 1,000 Jul-29-2023, 06:05 AM
Last Post: Girishbabu_27
  [split] Parse Nested JSON String in Python mmm07 4 1,549 Mar-28-2023, 06:07 PM
Last Post: snippsat
  python read iperf log and parse throughput jacklee26 4 2,803 Aug-27-2022, 07:04 AM
Last Post: Yoriz
  Feed List items with Integer euras 9 4,000 May-19-2021, 07:45 PM
Last Post: snippsat
  splitting UAV/sat images to smaller pieces in order to feed a CNN hobbyist 0 1,539 Dec-08-2020, 11:48 AM
Last Post: hobbyist
  Rotation Effect on live Webcam Feed Leziiy 0 1,620 Sep-12-2020, 04:25 PM
Last Post: Leziiy
  Replace every 20th space with line feed T_Lafferty 3 2,397 Jul-19-2020, 10:37 PM
Last Post: T_Lafferty
  Parse a REST API call using Python GKT 1 1,915 May-07-2020, 04:15 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