Simulate `bash` behaviour using Python and named pipes.

Gregory Ewing greg.ewing at canterbury.ac.nz
Fri Aug 9 21:07:03 EDT 2013


Luca Cerone wrote:
> Thanks! I managed to make it work using the threading library :)

If at least one of the external programs can accept the source
or destination as a filename argument instead of redirecting its
stdin or stdout, you can also do something like this:

import subprocess

p2 = subprocess.Popen(["cat", "named_pipe"])
pipe = open("named_pipe", "w")
p1 = subprocess.Popen(["ls", "-lah"], stdout = pipe)
pipe.close()
p1.wait()
p2.wait()

That works because opening the reading end of the pipe is
done by the subprocess executing cat, leaving the main
process free to open the other end.

-- 
Greg



More information about the Python-list mailing list