Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6665803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:45:42+00:00 2026-05-26T02:45:42+00:00

Maybe there’s someone out in the ether that can help me with this one.

  • 0

Maybe there’s someone out in the ether that can help me with this one. (I have seen a number of similar questions to this on SO, but none deal with both standard out and standard error or deal with a situation quite like mine, hence this new question.)

I have a python function that opens a subprocess, waits for it to complete, then outputs the return code, as well as the contents of the standard out and standard error pipes. While the process is running, I’d like to also display the output of both pipes as they are populated. My first attempt has resulted in something like this:

process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

stdout = str()
stderr = str()
returnCode = None
while True:
    # collect return code and pipe info
    stdoutPiece = process.stdout.read()
    stdout = stdout + stdoutPiece
    stderrPiece = process.stderr.read()
    stderr = stderr + stderrPiece
    returnCode = process.poll()

    # check for the end of pipes and return code
    if stdoutPiece == '' and stderrPiece == '' and returnCode != None:
        return returnCode, stdout, stderr

    if stdoutPiece != '': print(stdoutPiece)
    if stderrPiece != '': print(stderrPiece)

There’s a couple problems with this though. Because read() reads until an EOF, the first line of the while loop will not return until the subprocess closes the pipe.

I could replace the read() in favor of read(int) but the printed output is distorted, cut off at the end of the read characters. I could readline() as a replacement, but the printed output is distorted with alternating lines of output and errors when there are many of both that occur at the same time.

Perhaps there’s a read-until-end-of-buffer() variant that I’m not aware of? Or maybe it can be implemented?

Maybe it’s best to implement a sys.stdout wrapper as suggested in this answer to another post? I would only want to use the wrapper in this function, however.

Any other ideas from the community?

I appreciate the help! 🙂

EDIT: The solution really should be cross-platform, but if you have ideas that aren’t, please share them away to keep the brainstorming going.


For another one of my python subprocess head scratchers, take a look at another of my questions on accounting for subprocess overhead in timing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:45:43+00:00Added an answer on May 26, 2026 at 2:45 am

    Make the pipes non-blocking by using fcntl.fcntl, and use select.select to wait for data to become available in either pipe. For example:

    # Helper function to add the O_NONBLOCK flag to a file descriptor
    def make_async(fd):
        fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)
    
    # Helper function to read some data from a file descriptor, ignoring EAGAIN errors
    def read_async(fd):
        try:
            return fd.read()
        except IOError, e:
            if e.errno != errno.EAGAIN:
                raise e
            else:
                return ''
    
    process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    make_async(process.stdout)
    make_async(process.stderr)
    
    stdout = str()
    stderr = str()
    returnCode = None
    
    while True:
        # Wait for data to become available 
        select.select([process.stdout, process.stderr], [], [])
    
        # Try reading some data from each
        stdoutPiece = read_async(process.stdout)
        stderrPiece = read_async(process.stderr)
    
        if stdoutPiece:
            print stdoutPiece,
        if stderrPiece:
            print stderrPiece,
    
        stdout += stdoutPiece
        stderr += stderrPiece
        returnCode = process.poll()
    
        if returnCode != None:
            return (returnCode, stdout, stderr)
    

    Note that fcntl is only available on Unix-like platforms, including Cygwin.

    If you need it to work on Windows without Cygwin, it’s doable, but it’s much, much tougher. You’ll have to:

    • Use the pywin32 library to call through to the native Win32 API
    • Use SetNamedPipeHandleState with PIPE_NOWAIT to make the stdout and stderr pipes non-blocking
    • Use WaitForMultipleObjects instead of select to wait for data to become available
    • Use ReadFile to read the data
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I haven't exactly found the answer to this, maybe there is no best one.
I can't find any. Maybe there is one, anyways?
Maybe there was a better way to do this, but I have a list
are there any improvements where i can improve this code? Maybe there are some
Maybe there is a package to do this, but I have not been able
PHP manual doesn't say that there is one, but... maybe there's a hidden one?
Maybe there's no simple answer to this question, but I ask in case someone
I'm so sure that this must exist, but if it doesn't maybe there is
I have a question about righteous way of programming in Python... Maybe there can
Maybe there is a method that does this that I don't know about -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.