Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51971
| Date | 2013-08-05 17:08 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Simulate `bash` behaviour using Python and named pipes. |
| References | <a4c08e1c-2915-48ed-a65d-2e1b89f28878@googlegroups.com> <mailman.209.1375711382.1251.python-list@python.org> <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> <mailman.211.1375714293.1251.python-list@python.org> <fad1a298-993c-4935-9f32-7cf20ca63796@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.214.1375718915.1251.python-list@python.org> (permalink) |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Simulate `bash` behaviour using Python and named pipes. Luca Cerone <luca.cerone@gmail.com> - 2013-08-05 06:09 -0700
Re: Simulate `bash` behaviour using Python and named pipes. Paul Wiseman <poalman@gmail.com> - 2013-08-05 14:39 +0100
Re: Simulate `bash` behaviour using Python and named pipes. Luca Cerone <luca.cerone@gmail.com> - 2013-08-05 06:51 -0700
Re: Simulate `bash` behaviour using Python and named pipes. MRAB <python@mrabarnett.plus.com> - 2013-08-05 15:03 +0100
Re: Simulate `bash` behaviour using Python and named pipes. Luca Cerone <luca.cerone@gmail.com> - 2013-08-05 07:11 -0700
Re: Simulate `bash` behaviour using Python and named pipes. MRAB <python@mrabarnett.plus.com> - 2013-08-05 15:51 +0100
Re: Simulate `bash` behaviour using Python and named pipes. Luca Cerone <luca.cerone@gmail.com> - 2013-08-05 08:27 -0700
Re: Simulate `bash` behaviour using Python and named pipes. MRAB <python@mrabarnett.plus.com> - 2013-08-05 17:08 +0100
Re: Simulate `bash` behaviour using Python and named pipes. Luca Cerone <luca.cerone@gmail.com> - 2013-08-05 09:54 -0700
Re: Simulate `bash` behaviour using Python and named pipes. MRAB <python@mrabarnett.plus.com> - 2013-08-05 20:45 +0100
Re: Simulate `bash` behaviour using Python and named pipes. Luca Cerone <luca.cerone@gmail.com> - 2013-08-05 14:47 -0700
Re: Simulate `bash` behaviour using Python and named pipes. MRAB <python@mrabarnett.plus.com> - 2013-08-05 23:42 +0100
Re: Simulate `bash` behaviour using Python and named pipes. Luca Cerone <luca.cerone@gmail.com> - 2013-08-06 09:25 -0700
Re: Simulate `bash` behaviour using Python and named pipes. Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-08-10 13:07 +1200
Re: Simulate `bash` behaviour using Python and named pipes. Alister <alister.ware@ntlworld.com> - 2013-08-05 14:29 +0000
Re: Simulate `bash` behaviour using Python and named pipes. Luca Cerone <luca.cerone@gmail.com> - 2013-08-05 07:59 -0700
Re: Simulate `bash` behaviour using Python and named pipes. Neil Cerutti <neilc@norwich.edu> - 2013-08-05 16:00 +0000
csiph-web