Simulate `bash` behaviour using Python and named pipes.

MRAB python at mrabarnett.plus.com
Mon Aug 5 15:45:05 EDT 2013


On 05/08/2013 17:54, Luca Cerone wrote:
> Thanks this works (if you add shell=True in Popen).
> If I don't want to use shell = True, how can I redirect the stdout to named_pipe? Popen accepts an open file handle for stdout, which I can't open for writing because that blocks the process...
>
You're back to using separate threads for the reader and the writer.
The one that opens the pipe first will block until the other one opens
the other end.

>>
>>
>> os.mkfifo("named_pipe", 0777)
>>
>> ls_process = subprocess.Popen("ls -lah > named_pipe")
>>
>> pipe = open("named_pipe", "r")
>>
>> # Read the output of the subprocess from the pipe.
>>
>>
>>
>> When the subprocess terminates (look at the docs for Popen objects),
>>
>> close and delete the fifo.
>




More information about the Python-list mailing list