Reputation: 161
I am trying to print in realtime the output from a C++ exe started from python 3.6.6-x64 using subprocess module. I tried everything from google and other questions and I always get the output when the subprocess exited.
This is the code snippet:
try:
process = Popen(fullcmdtokens, stdout=PIPE, stderr=STDOUT, encoding='utf8', shell=True, errors='replace')
except Exception as exception:
print(exception)
else:
while process.poll() is None:
for lline in process.stdout:
process.stdout.flush()
edit_and_do_stuff(lline)
print(lline)
I also tried using
process.stdout.readline()
and
process.stdout.read(1)
and i modified the C++ exe to flush after every print.
Any ideas?
Upvotes: 0
Views: 194
Reputation: 12017
You can do it if you don’t send stdout to a pipe:
try:
process = Popen(fullcmdtokens, stderr=STDOUT, encoding='utf8', shell=True, errors='replace')
except Exception as exception:
print(exception)
If you need to change each line:
try:
process = Popen(fullcmdtokens, stdout=PIPE, stderr=STDOUT, encoding='utf8', shell=True, errors='replace')
while process.poll() is None:
for lline in process.stdout:
process.stdout.flush()
edit_and_do_stuff(lline)
print(lline)
except Exception as exception:
print(exception)
Upvotes: 1