Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
line-by-line output from a subprocess
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chermside, Michael  
View profile  
 More options May 23 2005, 1:06 pm
Newsgroups: comp.lang.python
From: "Chermside, Michael" <mcherms...@ingdirect.com>
Date: Mon, 23 May 2005 13:06:15 -0400
Local: Mon, May 23 2005 1:06 pm
Subject: line-by-line output from a subprocess
I am using the subprocess module to invoke a command-line utility and
process the output.

However, I would like to process the output line-by-line as it is
generated rather than
running the subprocess to completion and THEN processing the results.
So, for instance,
I'd like to write code like this:

    all_files = []
    import subprocess
    subp = subprocess.Popen('dir', stdout=subprocess.PIPE)
    for line in subp.stdout:
        sys.stdout.print(line)
        all_files.append(line)

...and have it print the lines to stdout one-by-one until the entire
list is
collected. (Pretend that 'dir' is VERY slow.)

The communicate() method in subprocess blocks until the subprocess has
exited... I'm sure there is some good reason for this behavior. But how
does
one achieve what I am trying to do?

(PS: this is on Windows... although I'd rather use an OS-agnostic
solution if
one exists.)

-- Michael Chermside

*************************************************************************** **
This email may contain confidential or privileged information. If you believe
 you have received the message in error, please notify the sender and delete
the message without copying or disclosing it.
*************************************************************************** **


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Percivall  
View profile  
 More options May 23 2005, 4:22 pm
Newsgroups: comp.lang.python
From: "Simon Percivall" <perciv...@gmail.com>
Date: 23 May 2005 13:22:04 -0700
Local: Mon, May 23 2005 4:22 pm
Subject: Re: line-by-line output from a subprocess
Okay, so the reason what you're trying to do doesn't work is that the
readahead buffer used by the file iterator is 8192 bytes, which clearly
might be too much. It also might be because the output from the
application you're running is buffered, so you might have to do
something about that as well.

Anyway, if the output from the child application is unbuffered, writing
a generator like this would work:

def iterread(fobj):
    stdout = fobj.stdout.read(1) # or what you like
    data = ""
    while stdout:
        data += stdout
        while "\n" in data:
            line, data = data.split("\n", 1)
            yield line
        stdout = fobj.stdout.read(1)
    if data:
        yield data,


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jp Calderone  
View profile  
(1 user)  More options May 23 2005, 4:58 pm
Newsgroups: comp.lang.python
From: Jp Calderone <exar...@divmod.com>
Date: Mon, 23 May 2005 20:58:00 GMT
Subject: Re: line-by-line output from a subprocess
On 23 May 2005 13:22:04 -0700, Simon Percivall <perciv...@gmail.com> wrote:

Or, doing the same thing, but with less code:

    def iterread(fobj):
        return iter(fobj.readline, '')

Haven't tried this on subprocess's pipes, but I assume they behave much the same way other file objects do (at least in this regard).

Jp


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Percivall  
View profile  
 More options May 23 2005, 9:02 pm
Newsgroups: comp.lang.python
From: "Simon Percivall" <perciv...@gmail.com>
Date: 23 May 2005 18:02:28 -0700
Local: Mon, May 23 2005 9:02 pm
Subject: Re: line-by-line output from a subprocess

Jp Calderone wrote:
> Or, doing the same thing, but with less code:

Hmm ... What have I been smoking?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google