У меня есть программа на C и Python. Python необходимо запустить программу C, а затем захватить выходные данные C неблокирующим способом. В этом примере программа на C будет выводить 10 строк в секунду и завершится через 5 секунд.
Ccode.c скомпилирован с помощью: gcc -D Arch_X86 ccode. c -o ccode.bin
Код: Выделить всё
#include
#include
#include
#include
uint64_t nanos(void){
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec*1000000000ull + ts.tv_nsec;
}
int main(){
printf("\nC Code Started\n");
uint64_t nowns, ns;
int16_t n=0;
nowns=nanos();
ns=nanos();
while ((nanos()-nowns) 100000000UL){
ns=nanos();
printf("From C I got %i\n", n);
n++;
}
}
printf("C code exit\n");
}
Код: Выделить всё
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import subprocess
import time
pr=subprocess.Popen( ['./ccode.bin'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
while True:
print (pr.stdout.read(1), end='' )
Подробнее здесь: https://stackoverflow.com/questions/790 ... utput-read