As suggested in an answer to my question "Python: start terminal program and parse its output while it's running" i am using a subprocess construct to communicate with a program:
server = subprocess.Popen(["meteor", "--port", port], stdout=subprocess.PIPE)
for line in iter(server.stdout.readline, ""):
# Do something for example
print(line)
The problem is: The subprocess (server) does not end each line with a newline directly as it is putting a (x2) at the end of a line, when the message duplicates.
But i want to react on every line update. So I want to handle \r
like \n
and not just iterate over finished lines, i want to iterate over the available string until the next "pause" wether it is a \r
or \n
or another line break to get the information directly when it appears, not only when the server sends a \n
. How can i do that?
maybe if you tried to use a non-blocking pipe connection. then read the stream much like a socket connection (in a while(true) loop)...hopefully this'll point you in the right direction.
from fcntl import fcntl, F_GETFL, F_SETFL
from os import O_NONBLOCK, read
server = subprocess.Popen(["meteor", "--port", port], stdout=subprocess.PIPE)
flags = fcntl(server.stdout, F_GETFL) # get current p.stdout flags
fcntl(server.stdout, F_SETFL, flags | O_NONBLOCK)
while True:
print read(server.stdout.fileno(), 1024),
you'll need to add error handling for a dropped connection or no more data.