I am trying to periodically read the most recent output of a subprocess
, to then process further elsewhere. I am not that experienced in subprocess
, maybe you have some best practices for me? The steps are:
- Start the process: This process should run in the background, so I will not see its whole output on the python terminal (I am afraid, that something is going to crash due to holding a lot of data in any variables of the python script). The process will produce data every second (a line with about 30 characters).
- Read latest data: In an infinite while-loop of the python script I like to get the latest line of the
subprocess
every 3 seconds.
import subprocess
import os
import time
os.chdir('/home/pi/Tutorials/RIOT/examples/gnrc_networking')
cmd = ["sudo", "BOARD=pba-d-01-kw2x", "make", "all", "flash", "term"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
while True:
line = p.stdout.readline().rstrip()
print(line)