Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'received:134': 0.05; 'indicated': 0.07; 'returned.': 0.07; 'tries': 0.07; 'subject:command': 0.09; 'true)': 0.09; 'python': 0.11; '2.7': 0.14; 'windows': 0.15; 'blocks': 0.16; 'pty': 0.16; 'slave': 0.16; 'slave,': 0.16; 'stderr': 0.16; 'stdout': 0.16; 'subject:program': 0.16; 'all.': 0.16; 'trying': 0.19; 'skip:p 40': 0.19; 'command': 0.22; 'import': 0.22; 'print': 0.22; 'header :User-Agent:1': 0.23; 'code:': 0.26; 'gets': 0.27; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'lines': 0.31; 'searches': 0.31; 'anyone': 0.31; 'run': 0.32; 'maybe': 0.34; 'subject:the': 0.34; 'skip:u 20': 0.35; 'but': 0.35; 'doing': 0.36; 'list': 0.37; 'work?': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'ian': 0.60; 'soon': 0.63; 'between': 0.67; 'results': 0.69; 'greetings': 0.72; 'pardon': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap8EAOUvF1KGuA9G/2dsb2JhbABawQ+CfIE1gxkBBXgRCyEWDwkDAgECATcBDRMIAogMrgaJEZBtFoQAA5dmhhaLRIMh Date: Fri, 23 Aug 2013 11:53:13 +0200 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Running a command line program and reading the result as it runs References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377251601 news.xs4all.nl 15947 [2001:888:2000:d::a6]:58807 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52875 Op 22-08-13 07:51, Ian Simcock schreef: > Greetings all. > > I'm using Python 2.7 under Windows and am trying to run a command line > program and process the programs output as it is running. A number of > web searches have indicated that the following code would work. > > import subprocess > > p = subprocess.Popen("D:\Python\Python27\Scripts\pip.exe list -o", > stdout=subprocess.PIPE, > stderr=subprocess.STDOUT, > bufsize=1, > universal_newlines=True, > shell=False) > for line in p.stdout: > print line > > When I use this code I can see that the Popen works, any code between > the Popen and the for will run straight away, but as soon as it gets to > the for and tries to read p.stdout the code blocks until the command > line program completes, then all of the lines are returned. > > Does anyone know how to get the results of the program without it blocking? Maybe the following can work? Untested code: from pty import openpty from subprocess import Popen master, slave = openpty() p = Popen("D:\Python\Python27\Scripts\pip.exe list -o", stdout = slave, stderr = slave, stdin = slave, close_fds = True) for line in master: print line The idea is to set a a pseudo terminal for pip so that the system thinks pip is doing IO with a terminal and so the IO will be line buffered. But all IO from pip will be available through the master in your program. -- Antoon Pardon