Simulate `bash` behaviour using Python and named pipes.

MRAB python at mrabarnett.plus.com
Mon Aug 5 10:51:35 EDT 2013


On 05/08/2013 15:11, Luca Cerone wrote:
> Hi MRAB, thanks for the reply!
>>
>> Opening the pipe for reading will block until it's also opened for
>> writing, and vice versa.
>>
>
> OK.
>
>>
>> In your bash code, 'ls' blocked until you ran 'cat', but because you
>> ran 'ls' in the background you didn't notice it!
>>
>>
> Right.
>>
>> In your Python code, the Python thread blocked on opening the pipe for
>> writing. It was waiting for another thread or process to open the pipe
>> for reading.
>
> OK. What you have written makes sense to me. So how do I overcome the block?
> As you said in bash I run the ls process in background. How do I do that in Python?
>
You need to ensure that the pipe is already open at the other end.

Why are you using a named pipe anyway?

If you're talking to another program, then that needs to be running
already, waiting for the connection, at the point that you open the
named pipe from this end.

If you're using a pipe _within_ a program (a queue would be better),
then you should opening for writing in one thread and for reading in
another.




More information about the Python-list mailing list