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: 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; '<': 0.19; '8bit%:5': 0.22; 'input': 0.22; 'example': 0.22; 'import': 0.22; '(in': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'file.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 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: References: Date: Mon, 5 Aug 2013 14:39:24 +0100 Subject: Re: Simulate `bash` behaviour using Python and named pipes. From: Paul Wiseman To: Luca Cerone 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --001a11c297debd243604e3336f79 Content-Type: text/plain; charset=ISO-8859-1 On 5 August 2013 14:09, Luca Cerone 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 > --001a11c297debd243604e3336f79 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 5 August 2013 14:09, Luca Cerone <luca.cerone@gmail.com> wrote:


You can pipe using subprocess

p1 = =3D subprocess.Popen(["ls", "-lah"], stdout=3Dsubproces= s.PIPE)
p2 =3D subprocess.Popen(["cat"], stdin=3Dp1.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:
=A0 =A0 p1 =3D subprocess.Popen(["ls", &= quot;-lah"], stdout=3Dnamed_pipe)
=A0 =A0 p2 =3D subprocess.= Popen(["cat"], stdin=3Dnamed_pipe)
=A0 =A0 p1.wait()
=A0 =A0 p2.wait()
= =A0
Luca
--
http://mail.python.org/mailman/listinfo/python-list

--001a11c297debd243604e3336f79--