Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; 'everybody,': 0.07; 'advance': 0.07; 'bash': 0.09; 'subject:using': 0.09; 'python': 0.11; 'thread': 0.14; 'blocked': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'help!!!': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'stdout': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'code,': 0.22; 'input': 0.22; 'example': 0.22; 'import': 0.22; '(in': 0.22; 'header:User-Agent:1': 0.23; 'header :In-Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'point': 0.28; 'external': 0.29; 'sets': 0.30; 'pipe': 0.31; 'another': 0.32; 'open': 0.33; 'linux': 0.33; "can't": 0.35; 'received:84': 0.35; 'but': 0.35; 'vice': 0.36; 'next': 0.36; "didn't": 0.36; 'thanks': 0.36; 'example,': 0.37; 'implement': 0.38; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'launch': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'commands': 0.60; 'simple': 0.61; 'such': 0.63; 'it!': 0.67; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; '(still': 0.84; 'reply- to:addr:python.org': 0.84; 'writing,': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=XqLDZz19 c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=K2DDQYBT4xIA:10 a=zc0myEFsKt8A:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=5F5fH2KFBC4A:10 a=bERAouOogXKSjWcLY0gA:9 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Mon, 05 Aug 2013 15:03:02 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Simulate `bash` behaviour using Python and named pipes. References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375711383 news.xs4all.nl 16009 [2001:888:2000:d::a6]:48831 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51960 On 05/08/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!!! > Opening the pipe for reading will block until it's also opened for writing, and vice versa. In your bash code, 'ls' blocked until you ran 'cat', but because you ran 'ls' in the background you didn't notice it! In your Python code, the Python thread blocked on opening the pipe for writing. It was waiting for another thread or process to open the pipe for reading.