Emulating pipes

Christopher T King squirrel at WPI.EDU
Thu Jul 29 20:27:27 CEST 2004

On Thu, 29 Jul 2004 brianc at temple.edu wrote:

> Does anyone know of a way I can make a program read/write from
> stdin/stdout so that I can chain it with other commands and interact
> with it from python?

> I would like to do something like 'otherprog | program stdin stdout |
> anotherprog' to take advantage of of the multiprocessor environment I'm
> on.

There are a few command-line solutions to this problem:

IRIX may or may not provide the /dev/stdin and /dev/stdout devices.  These 
were introduced (?) by Linux as a means to access the current process's 
stdin and stdout file descriptors, respectively:

$ otherprog | program /dev/stdin /dev/stdout | anotherprog

If IRIX doesn't provide this, and you are using bash (or something based
on it), you could use process substitution.  This is a magic construction
that allows you to use programs where files are expected:

$ program <(otherprog) >(anotherprog)

If that fails, your last resort is to use named FIFOs (this is one way 
process substitution is implemented, the other being similar to the 
first solution):

$ mkfifo tmpfifo1
$ mkfifo tmpfifo2
$ otherprog > tmpfifo1 & program tmpfifo1 tmpfifo2 & anotherprog < tmpfifo2

Hope this helps.

More information about the Python-list mailing list