I'm trying to generate numbers in C, then pass them onto python, and print the data using sys.stdin. The code for generating numbers in C is this,
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
int main() {
unsigned int i;
srand(time(NULL));
while (1) {
i = random() % 15000;
printf("%u.%03u", i / 1000, i % 1000);
printf("\n");
usleep(100 * 1000);
}
}
and the program for reading and printing this in python is this:
import sys
while(1):
for line in sys.stdin:
print(line[:-1])
Then, after I compile the c file gcc gen.c
, I pipe it into python ~/a.out | python3 new.py
. However, this only works when I delete the usleep part in the C code. When I delete the usleep bit, it works fine, but with the usleep part, it does not print anything and it seems that it gets stuck in line 4 in new.py.