I've got this C program:
#include <stdio.h>
#include <Windows.h>
int main() {
for (int i=0;; i++) {
printf("%i\n",i);
Sleep(100);
}
return 0;
}
And I have a Python script that tries to capture its output and do something with it:
from subprocess import Popen, PIPE
p = Popen("a.exe", stdout=PIPE, shell=True)
print p.stdout.readline()
... and it hangs on the last line without printing anything on a screen.
I've tried to solve this problem using the Python shell and found this:
>>> from test import *
>>> p.stdout.flush()
>>> p.stdout.readline()
'0\r\n'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
^CKeyboardInterrupt
It can actually read the output but only when I send KeyboardInterrupt. p.stdout.read(1)
behaves the same way.
So, what's a working, correct way to do what I want?
Edit:
Ok, looks like it is impossible on Windows, see comments to first answer.