Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51958

Re: Simulate `bash` behaviour using Python and named pipes.

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <poalman@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'subject:Python': 0.06; 'everybody,': 0.07; 'advance': 0.07; 'subject:using': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'help!!!': 0.16; 'stdout': 0.16; 'wrote:': 0.18; 'trying': 0.19; '&lt;': 0.19; '8bit%:5': 0.22; 'input': 0.22; 'example': 0.22; 'import': 0.22; '(in': 0.22; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'file.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '&gt;': 0.26; 'pass': 0.26; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'point': 0.28; 'external': 0.29; 'sets': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'pipe': 0.31; 'file': 0.32; 'open': 0.33; 'linux': 0.33; 'url:python': 0.33; "can't": 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'next': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'example,': 0.37; 'implement': 0.38; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'rather': 0.38; 'launch': 0.39; 'url:mail': 0.40; 'how': 0.40; 'commands': 0.60; 'august': 0.61; 'skip:o 30': 0.61; 'simple': 0.61; 'such': 0.63; 'to:addr:gmail.com': 0.65; '(still': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=U6u14fKYybA7/PYarhrnXsYcTnPjyRDiygF/Gi8hb/g=; b=w7mDPUsCCZyYM9cwxMQYFSWz8beJlluRg5mBj63nzpFqbJnKszxjRJqgMBtfdfDL+D wIlBTfMH2GlXD3LZqJ4kLuqMbLA7I/xZgoE/p9y2RMh4s86tFFqi19J55qcJMkSzBjVx 6dU18eG1rs3xZ+mY7BoKmp0baPnXtnamGjJIQHLJ3GU+IyOPOMHP2sYwjOYARbNNAXBq rNoXETT51emmv4myeHzgL8gEMrRnx5YHoJl7kWlpnlKeX75D0LYR52zzTkt3Nu9JHmPX N6FFKX1hlFFCGY3u2JxI28ryWVs5adWa9mNNV55vU+JSfOg8TkSqr8cIS/xaLmuUyIZX 4l/A==
MIME-Version 1.0
X-Received by 10.194.249.103 with SMTP id yt7mr13129670wjc.80.1375709964344; Mon, 05 Aug 2013 06:39:24 -0700 (PDT)
In-Reply-To <a4c08e1c-2915-48ed-a65d-2e1b89f28878@googlegroups.com>
References <a4c08e1c-2915-48ed-a65d-2e1b89f28878@googlegroups.com>
Date Mon, 5 Aug 2013 14:39:24 +0100
Subject Re: Simulate `bash` behaviour using Python and named pipes.
From Paul Wiseman <poalman@gmail.com>
To Luca Cerone <luca.cerone@gmail.com>
Content-Type multipart/alternative; boundary=001a11c297debd243604e3336f79
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.208.1375709966.1251.python-list@python.org> (permalink)
Lines 123
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375709966 news.xs4all.nl 15945 [2001:888:2000:d::a6]:42048
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51958

Show key headers only | View raw


[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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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