Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'ideally': 0.04; 'output': 0.05; 'subject:Python': 0.06; 'executes': 0.09; 'subject:using': 0.09; 'terminates': 0.09; 'runs': 0.10; 'python': 0.11; 'bug': 0.12; 'thread': 0.14; '"r")': 0.16; 'already,': 0.16; 'email)': 0.16; 'example)': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; ':-)': 0.16; 'wrote:': 0.18; '(the': 0.22; 'command': 0.22; 'header:User-Agent:1': 0.23; 'parse': 0.24; 'stick': 0.24; 'developers': 0.25; '(see': 0.26; 'equivalent': 0.26; 'suggested': 0.26; 'this:': 0.26; 'second': 0.26; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'function': 0.29; 'external': 0.29; 'program,': 0.31; 'another.': 0.31; 'explained': 0.31; 'extract': 0.31; 'pipe': 0.31; 'workaround': 0.31; 'run': 0.32; 'another': 0.32; 'open': 0.33; 'running': 0.33; 'entirely': 0.33; 'could': 0.34; "can't": 0.35; 'tool': 0.35; 'something': 0.35; 'thanks': 0.36; 'should': 0.36; 'example,': 0.37; 'skip:o 20': 0.38; 'writes': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'delete': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'ensure': 0.60; 'read': 0.60; "you're": 0.61; 'taking': 0.65; 'talking': 0.65; 'temporary': 0.65; 'close': 0.67; 'header:Reply- To:1': 0.67; 'reads': 0.68; 'reply-to:no real name:2**0': 0.71; 'end.': 0.84; 'occasion': 0.84; 'redirecting': 0.84; 'reply- to:addr:python.org': 0.84; 'on?': 0.91; 'connection,': 0.95 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=KrN0hwmN 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=rgjiglNof1XOxZ51FZIA:9 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Mon, 05 Aug 2013 17:08:32 +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: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375718915 news.xs4all.nl 15867 [2001:888:2000:d::a6]:40091 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51971 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.