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


Groups > comp.lang.python > #52877

RE: Running a command line program and reading the result as it runs

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jcasale@activenetwerx.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'indicated': 0.07; 'returned.': 0.07; 'tries': 0.07; 'subject:command': 0.09; 'python': 0.11; '2.7': 0.14; 'windows': 0.15; 'blocks': 0.16; 'received:172.18.0': 0.16; 'subject:program': 0.16; 'trying': 0.19; 'command': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'this:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'lines': 0.31; 'searches': 0.31; 'anyone': 0.31; 'run': 0.32; 'subject:the': 0.34; 'skip:u 20': 0.35; 'but': 0.35; 'charset:us-ascii': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'received:unknown': 0.61; 'soon': 0.63; 'between': 0.67; 'results': 0.69
X-Cloudmark-SP-Filtered true
X-Cloudmark-SP-Result v=1.1 cv=+lz5uX4xiInwtrymCIFxsZpC13k2qgqBCqxhxbRs02Y= c=1 sm=1 a=AgG5ixNBvo4A:10 a=pBS3jtf3S80A:10 a=7PYXob_7ZXMA:10 a=BLceEmwcHowA:10 a=kj9zAlcOel0A:10 a=xqWC_Br6kY4A:10 a=oNw28mxuUhXRB3mVwYQ4Ag==:17 a=C-EBSsR1WfTbUoOYiyYA:9 a=CjuIK1q_8ugA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117
From "Joseph L. Casale" <jcasale@activenetwerx.com>
To "python-list@python.org" <python-list@python.org>
Subject RE: Running a command line program and reading the result as it runs
Thread-Topic Running a command line program and reading the result as it runs
Thread-Index AQHOn+yekPVm770Vd0iR/DxbLoq+WpminPcw
Date Fri, 23 Aug 2013 10:50:18 +0000
References <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> <52173109.5090304@rece.vub.ac.be> <52173AD2.4050509@rece.vub.ac.be>
In-Reply-To <52173AD2.4050509@rece.vub.ac.be>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-originating-ip [172.18.0.201]
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
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.160.1377255090.19984.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1377255090 news.xs4all.nl 15998 [2001:888:2000:d::a6]:45818
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52877

Show key headers only | View raw


> >> 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?

Try this:

p = subprocess.Popen(args, stdout=subprocess.PIPE)
for line in p.stdout:
    print(line)
p.wait()

jlc

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Running a command line program and reading the result as it runs Ian Simcock <Ian.Simcock@Internode.on.net> - 2013-08-22 15:21 +0930
  Re: Running a command line program and reading the result as it runs Chris Angelico <rosuav@gmail.com> - 2013-08-22 16:22 +1000
    Re: Running a command line program and reading the result as it runs Ian Simcock <Ian.Simcock@Internode.on.net> - 2013-08-23 00:56 +0930
      Re: Running a command line program and reading the result as it runs Chris Angelico <rosuav@gmail.com> - 2013-08-23 01:33 +1000
        Re: Running a command line program and reading the result as it runs Ian Simcock <Ian.Simcock@Internode.on.net> - 2013-08-23 16:22 +0930
        Re: Running a command line program and reading the result as it runs Grant Edwards <invalid@invalid.invalid> - 2013-08-23 14:02 +0000
  Re: Running a command line program and reading the result as it runs Rob Wolfe <rw@smsnet.pl> - 2013-08-22 23:14 +0200
    Re: Running a command line program and reading the result as it runs Ian Simcock <Ian.Simcock@Internode.on.net> - 2013-08-23 16:31 +0930
  Re: Running a command line program and reading the result as it runs Gertjan Klein <gklein@xs4all.nl> - 2013-08-23 11:32 +0200
  Re: Running a command line program and reading the result as it runs Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-08-23 11:53 +0200
  Re: Running a command line program and reading the result as it runs Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-08-23 12:34 +0200
  RE: Running a command line program and reading the result as it runs "Joseph L. Casale" <jcasale@activenetwerx.com> - 2013-08-23 10:50 +0000
  Re: Running a command line program and reading the result as it runs Peter Otten <__peter__@web.de> - 2013-08-23 13:14 +0200
    Re: Running a command line program and reading the result as it runs Gertjan Klein <gklein@xs4all.nl> - 2013-08-23 14:03 +0200
    Re: Running a command line program and reading the result as it runs Ian Simcock <Ian.Simcock@Internode.on.net> - 2013-08-24 19:06 +0930
  Re: Running a command line program and reading the result as it runs random832@fastmail.us - 2013-08-23 12:04 -0400
  Re: Running a command line program and reading the result as it runs Peter Otten <__peter__@web.de> - 2013-08-23 18:39 +0200

csiph-web