Real Time Output Issue resolved: I did encountered similar issue in Python, while capturing the real time output from c program. I added "fflush(stdout);" in my C code. It worked for me. Here is the snip the code
<< C Program >>
#include <stdio.h>
void main()
{
int count = 1;
while (1)
{
printf(" Count %d\n", count++);
fflush(stdout);
sleep(1);
}
}
<< Python Program >>
#!/usr/bin/python
import os, sys
import subprocess
procExe = subprocess.Popen(".//count", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
while procExe.poll() is None:
line = procExe.stdout.readline()
print("Print:" + line)
<< OUTPUT>> Print: Count 1 Print: Count 2 Print: Count 3
UPDATE::
The above one has flaws as we are reading only oneline per poll iteration.
So for bursty output from the command, this wont work as only one line will be read and rest of the lines in the burst will not be read.
Using select module handles both bursty and isochronous output from command.
UPDATE::
The above one has flaws as we are reading only oneline per poll iteration.
So for bursty output from the command, this wont work as only one line will be read and rest of the lines in the burst will not be read.
Using select module handles both bursty and isochronous output from command.
import select; procExe = subprocess.Popen(self.runCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE , stdin=subprocess.PIPE, universal_newlines=True )# , creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) self.childProcess['simv'] = procExe readable = { procExe.stdout.fileno(): sys.stdout.buffer, # log separately procExe.stderr.fileno(): sys.stdout.buffer, } while readable: for fd in select(readable, [], [])[0]: data = os.read(fd,30000) # data = fd.readline().encode() if not data: del readable[fd] else: s = ""+data.decode() print(s)
Comments
Post a Comment