On 05/08/2013 16:27, Luca Cerone wrote:
Thanks MRAB,
You need to ensure that the pipe is already open at the other end.
So I need to open the process that reads the pipe before writing in
it?
Why are you using a named pipe anyway?
For some bug in ipython (see my previous email) I can't use
subprocess.Popen and pipe in the standard way. One of Ipython
developers has suggested me to use named pipes as a temporary
workaround. So I am taking the occasion to learn :)
An alternative workaround is to use CPython. :-)
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.
I am not entirely sure I got this: ideally I would like to have a
function that runs an external tool (the equivalent of ls in my
example) redirecting its output in a named pipe.
A second function (the cat command in my example) would read the
named_pipe, parse it and extract some features from the output.
I also would like that the named_pipe is deleted when the whole
communication is ended.
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.
Let's stick with the pipe :) I will ask about the queue when I
manage to use pipes ;)
I should have explained better that I have no idea how to run
threads in Python :): how do I open a thread that executes "ls -lah"
in background and writes into a named pipe? And how do I open a
thread that reads from the named pipe?
Can you please post a small example, so that I have something to
work on?
You could try something like this:
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.
--
http://mail.python.org/mailman/listinfo/python-list