Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51958
| References | <a4c08e1c-2915-48ed-a65d-2e1b89f28878@googlegroups.com> |
|---|---|
| Date | 2013-08-05 14:39 +0100 |
| Subject | Re: Simulate `bash` behaviour using Python and named pipes. |
| From | Paul Wiseman <poalman@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.208.1375709966.1251.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On 5 August 2013 14:09, Luca Cerone <luca.cerone@gmail.com> wrote:
> Hi everybody,
> I am trying to understand how to use named pipes in python to launch
> external processes (in a Linux environment).
>
> As an example I am trying to "imitate" the behaviour of the following sets
> of commands is bash:
>
> > mkfifo named_pipe
> > ls -lah > named_pipe &
> > cat < named_pipe
>
> In Python I have tried the following commands:
>
> import os
> import subprocess as sp
>
> os.mkfifo("named_pipe",0777) #equivalent to mkfifo in bash..
> fw = open("named_pipe",'w')
> #at this point the system hangs...
>
> My idea it was to use subprocess.Popen and redirect stdout to fw...
> next open named_pipe for reading and giving it as input to cat (still
> using Popen).
>
> I know it is a simple (and rather stupid) example, but I can't manage to
> make it work..
>
>
> How would you implement such simple scenario?
>
> Thanks a lot in advance for the help!!!
>
>
You can pipe using subprocess
p1 = subprocess.Popen(["ls", "-lah"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["cat"], stdin=p1.stdout)
p1.wait()
p2.wait()
You can also pass a file object to p1's stdout and p2's stdin if you want
to pipe via a file.
with open("named_pipe", "rw") as named_pipe:
p1 = subprocess.Popen(["ls", "-lah"], stdout=named_pipe)
p2 = subprocess.Popen(["cat"], stdin=named_pipe)
p1.wait()
p2.wait()
> Luca
> --
> http://mail.python.org/mailman/listinfo/python-list
>
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