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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:15:32+00:00 2026-05-20T22:15:32+00:00

So, I have been facing a problem with using subprocess for a python app

  • 0

So, I have been facing a problem with using subprocess for a python app i am writing. To illustrate the problem, I wrote this small script that replicates my problem pretty well.

from __future__ import print_function

import subprocess as sp
from select import select

p = sp.Popen(['ls'], stdout=sp.PIPE, stderr=sp.PIPE, stdin=sp.PIPE)
p.stdin.close()

while p.returncode is None or p.stdout.closed or p.stderr.closed:
    # print('returncode is', p.returncode)
    available_readers = select([p.stdout, p.stderr], [], [], 2.0)[0]
    for r in available_readers:
        print(r.read(1))
        # output_display.insert(tk.END, r.read(1))

Ofcourse, we all know that ls command exists immediately after printing some stuff to stdout (or may be stderr), but the above script never exists.

As you can see from the last line (comment) in the above script, I have to put the content from the subprocess into a tk text component. So, I can’t use methods like .communicate and other blocking calls as the command I need to run takes a long time and I need to show the output (almost) realtime. (Ofcourse, I have to run this in a separate thread when running Tk, but that’s something else).

So, I am unable to understand why this script never exits. It keeps printing empty strings forever (after the expected output of the ls command).

Please advise. I am running python 2.6.6 on ubuntu 10.10

Edit: Here’s the version of the above script that works

from __future__ import print_function

import subprocess as sp
from select import select

p = sp.Popen(['ls'], stdout=sp.PIPE, stderr=sp.PIPE, stdin=sp.PIPE)
p.stdin.close()

while p.poll() is None:
    # print('returncode is', p.returncode)
    available_readers = select([p.stdout, p.stderr], [], [], 2.0)[0]
    for r in available_readers:
        print(r.read(1), end='')
        # output_display.insert(tk.END, r.read(1))

print(p.stdout.read(), end='')
print(p.stderr.read(), end='')
  • 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-20T22:15:33+00:00Added an answer on May 20, 2026 at 10:15 pm
    while p.returncode is None or p.stdout.closed or p.stderr.closed:
    

    loops while any of the conditions is true. You probably meant to check just returncode (and poll in every iteration).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been facing so many problem using the anonymous class like I can't
We have been facing a small problem in MySQL procedure. We have placed some
We have been facing Out of Memory errors in our App server for sometime.
Hell All, So I have been facing this behavior for quite some time and
I have been facing a strange problem while returning dataset through WCF service (WSHttpBinding).
Since some days, I have been facing a problem in Visual Studio 2008, related
The problem I am facing is that I have a glx based application that
Recently, I have been facing a problem with throwing exception in JDBC topic. I
My very first post here! I have been using this site for many years
I have been facing a problem in InstallScript to get the list of registry

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.