Home > Net >  Feeding data from C to Python in Ubuntu
Feeding data from C to Python in Ubuntu

Time:05-10

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.u", 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.

CodePudding user response:

You just need to flush the output. The reason is explained in Why does stdout need explicit flushing when redirected to file?, i.e. the piping changes the buffering behavior.

#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.u", i / 1000, i % 1000);
                printf("\n");
                fflush(stdout);
                usleep(1000 * 1000);

        }
}
$ gcc gen.c -o gen       
$ ./gen  | python3 new.py
14.383
0.886
  • Related