Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force another program's standard output to be unbuffered using Python

A python script is controlling an external application on Linux, passing in input via a pipe to the external applications stdin, and reading output via a pipe from the external applications stdout.

The problem is that writes to pipes are buffered by block, and not by line, and therefore delays occur before the controlling script receives data output by, for example, printf in the external application.

The external application cannot be altered to add explicit fflush(0) calls.

How can the pty module of the python standard library be used with the subprocess module to achieve this?

like image 231
grrussel Avatar asked Oct 09 '09 14:10

grrussel


1 Answers

You can use PTYs to solve this by:

  • Creating a pty master/slave pair;
  • Connecting the child process's stdin, stdout and stderr to the pty slave device;
  • Reading from and writing to the pty master in the parent.
like image 175
caf Avatar answered Sep 22 '22 18:09

caf