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 8307979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:39:58+00:00 2026-06-08T18:39:58+00:00

There are a lot of similar posts, but I didn’t find answer. On Gnu/Linux,

  • 0

There are a lot of similar posts, but I didn’t find answer.

On Gnu/Linux, with Python and subprocess module, I use the following code to iterate over the
stdout/sdterr of a command launched with subprocess:

class Shell:
    """ 
    run a command and iterate over the stdout/stderr lines
    """

    def __init__(self):

        pass

    def __call__(self,args,cwd='./'):

        p = subprocess.Popen(args,
                cwd=cwd, 
                stdout = subprocess.PIPE,
                stderr = subprocess.STDOUT,
                )

        while True:

            line = p.stdout.readline()
            self.code = p.poll()

            if line == '':
                if self.code != None:
                    break
                else:
                    continue

            yield line

#example of use
args = ["./foo"]
shell = Shell()
for line in shell(args):
     #do something with line
     print line,

This works fine… except if the command executed is python, for example `args = [‘python’,’foo.py’], in which case the output is not flushed but printed only when the command is finished.

Is there a solution?

  • 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-06-08T18:40:00+00:00Added an answer on June 8, 2026 at 6:40 pm

    Check out How to flush output of Python print?.

    You need to run the python subprocess with the -u option:

    -u Force stdin, stdout and stderr to be totally unbuffered. On sys‐
    tems where it matters, also put stdin, stdout and stderr in binary
    mode. Note that there is internal buffering in xreadlines(),
    readlines() and file-object iterators (“for line in sys.stdin”)
    which is not influenced by this option. To work around this, you
    will want to use “sys.stdin.readline()” inside a “while 1:” loop.

    Or, if you have control over the python sub-process script you can use sys.stdout.flush() to flush the output every time you print.

    import sys
    sys.stdout.flush()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there a lot of similar questions to this, but I didn't find
There's a lot of similar questions, but I didn't see this: I'm using users
I know there are a lot of similar questions, but couldn't find a value
There is lot of similar questions at StackOverflow, but I can't find any answered
There are a lot of similar questions, but, probably, mine is a little bit
I know there are a lot of similar questions on SF, but I think
There are a lot of questions similar to this but none dealing with webforms
I know there are a lot of questions similar to mine, but I actually
I know similar questions come up a lot and there's probably no definitive answer,
I've spent a lot of hours searching and reading similar posts, but none of

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.