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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:51:28+00:00 2026-05-30T00:51:28+00:00

I wrote this little test class, based upon a Python issue – closed/fixed ,

  • 0

I wrote this little test class, based upon a Python issue – closed/fixed, and it seems to be occurring in Python 2.7.1 on Fedora 15.

from subprocess import Popen, PIPE
from threading import Thread

OUTPUT = "asl;dkfhqwiouethnjzxvnhsldfhuyasdhofuiweqamchuisemfawepfhuaipwemhfuaehfclkuehnflw ehfcoiuwehfiuwmhefuiwehfoiuhSfcl hfulh fuiqhuif huiwafh uiahf iUH fhh flkJH fl HASLFuhSAIULFhSUA HFulSAHfOI SUFChiuwqhncriouweycriuowanbyoUIWCryu iWyruawyrouiWYRcoiu YCRoiuNr uyr oUIAWryocIUWRNyc owuroiuNr cuWyrnawueitcnoy U IuiR yiuowaYnorc oWIUAr coiury iuoAW rnuoi asdfsdfd\n"


class X(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        print("Running")
        for i in xrange(10):
            s = Popen(
                "cat /tmp/junk",
                shell=True,
                stdout=PIPE,
                universal_newlines=True
            )
            output = s.communicate()[0]
            if not output == OUTPUT:
                print("Error: %r" % output)


XThreads = set([])

for i in xrange(1000):
    XThreads.add(X())

for x in XThreads:
    x.start()

Just create a file, /tmp/junk in this case, that has the contents of OUTPUT, less the last newline.

Running this, you would expect to see “Running” on every line. However, sometimes it shows ” Running” or “RunningRunning\n\nRunning”.

(Removed citation to actual problem, as this was a false-symptom, thanks to @phihag’s answer).

The actual issue: https://stackoverflow.com/questions/9338409/python-subprocess-popen-corrupts-binary-streams

  • 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-30T00:51:30+00:00Added an answer on May 30, 2026 at 12:51 am

    The behavior you’re seeing has nothing to do with subprocess; I can reproduce it with:

    import threading
    def run(): print("Running")
    for x in [threading.Thread(target=run) for i in range(1000)]:
        x.start()
    

    This is because Python’s print is not thread-safe. To avoid the race condition between printing out text and newline, directly write to stdout, like this:

    import threading,sys
    def run(): sys.stdout.write("Running\n")
    for x in [threading.Thread(target=run) for i in range(1000)]:
        x.start()
    

    This assumes the underlying write calls to stdout are thread-safe. That depends on the platform.

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

Sidebar

Related Questions

I've got this little class I wrote to try playing with custom __getattr__ methods,
First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to
I wrote this function for filling closed loop, pixvali is declared globally to store
I started a little experiment today: I wrote a C++ class which depends on
I wrote a little test program to show here. Here is the source code.
I wrote a little test to check for null pointer, I simplified it with
I have this little function function makewindows(){ child1 = window.open (about:blank); child1.document.write(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']),
I am a little confused on the logic of how to write this SQL
This is going to be a little tricky to explain. I'm trying to write
I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000" .

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.