F
F
Ffff Gggg2021-08-19 21:30:07
Python
Ffff Gggg, 2021-08-19 21:30:07

How to make Popen output immediately display?

Faced a problem, the answer to which I can not find for several days. I have a small program written in C and I want to execute it using python. I used subprocess.Popen, I output data through communicate, but, as far as I understand, the output data is buffered and the process waits for the end of the program and only then outputs the data. This would not be a problem, but in my program, user input is expected and there is no way for the user to know what the blinking cursor wants from him.
In general, how can I take data out of a process as it comes in, rather than when the process ends?
My python code:

import subprocess

options = input("$ ")

run_program = subprocess.Popen(options, stdout=subprocess.PIPE)
output = run_program.stdout.read()

print(output.decode('utf-8'))

And if you're interested, the C code:
#include <stdio.h>

int main() {
  int a = 5;
  int b = 5;
  int sum = a + b;
  int blank;

  printf("Write some number: ");
  scanf("%d", &blank);

  printf("%d\n", blank);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question