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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:01:11+00:00 2026-06-13T03:01:11+00:00

Very specific question (I hope): What are the differences between the following three codes?

  • 0

Very specific question (I hope): What are the differences between the following three codes?

(I expect it to be only that the first does not wait for the child process to be finished, while the second and third ones do. But I need to be sure this is the only difference…)

I also welcome other remarks/suggestions (though I’m already well aware of the shell=True dangers and cross-platform limitations)

Note that I already read Python subprocess interaction, why does my process work with Popen.communicate, but not Popen.stdout.read()? and that I do not want/need to interact with the program after.

Also note that I already read Alternatives to Python Popen.communicate() memory limitations? but that I didn’t really get it…

Finally, note that I am aware that somewhere there is a risk of deadlock when one buffer is filled with one output using one method, but I got lost while looking for clear explanations on the Internet…

First code:

from subprocess import Popen, PIPE

def exe_f(command='ls -l', shell=True):
    """Function to execute a command and return stuff"""

    process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)

    stdout = process.stdout.read()
    stderr = process.stderr.read()

    return process, stderr, stdout

Second code:

from subprocess import Popen, PIPE
from subprocess import communicate

def exe_f(command='ls -l', shell=True):
    """Function to execute a command and return stuff"""

    process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)

    (stdout, stderr) = process.communicate()

    return process, stderr, stdout

Third code:

from subprocess import Popen, PIPE
from subprocess import wait

def exe_f(command='ls -l', shell=True):
    """Function to execute a command and return stuff"""

    process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)

    code   = process.wait()
    stdout = process.stdout.read()
    stderr = process.stderr.read()

    return process, stderr, stdout
  • 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-13T03:01:12+00:00Added an answer on June 13, 2026 at 3:01 am

    If you look at the source for subprocess.communicate(), it shows a perfect example of the difference:

    def communicate(self, input=None):
        ...
        # Optimization: If we are only using one pipe, or no pipe at
        # all, using select() or threads is unnecessary.
        if [self.stdin, self.stdout, self.stderr].count(None) >= 2:
            stdout = None
            stderr = None
            if self.stdin:
                if input:
                    self.stdin.write(input)
                self.stdin.close()
            elif self.stdout:
                stdout = self.stdout.read()
                self.stdout.close()
            elif self.stderr:
                stderr = self.stderr.read()
                self.stderr.close()
            self.wait()
            return (stdout, stderr)
    
        return self._communicate(input)
    

    You can see that communicate does make use of the read calls to stdout and stderr, and also calls wait(). It is just a matter of order of operations. In your case because you are using PIPE for both stdout and stderr, it goes into _communicate():

    def _communicate(self, input):
        stdout = None # Return
        stderr = None # Return
    
        if self.stdout:
            stdout = []
            stdout_thread = threading.Thread(target=self._readerthread,
                                             args=(self.stdout, stdout))
            stdout_thread.setDaemon(True)
            stdout_thread.start()
        if self.stderr:
            stderr = []
            stderr_thread = threading.Thread(target=self._readerthread,
                                             args=(self.stderr, stderr))
            stderr_thread.setDaemon(True)
            stderr_thread.start()
    
        if self.stdin:
            if input is not None:
                self.stdin.write(input)
            self.stdin.close()
    
        if self.stdout:
            stdout_thread.join()
        if self.stderr:
            stderr_thread.join()
    
        # All data exchanged.  Translate lists into strings.
        if stdout is not None:
            stdout = stdout[0]
        if stderr is not None:
            stderr = stderr[0]
    
        # Translate newlines, if requested.  We cannot let the file
        # object do the translation: It is based on stdio, which is
        # impossible to combine with select (unless forcing no
        # buffering).
        if self.universal_newlines and hasattr(file, 'newlines'):
            if stdout:
                stdout = self._translate_newlines(stdout)
            if stderr:
                stderr = self._translate_newlines(stderr)
    
        self.wait()
        return (stdout, stderr)
    

    This uses threads to read from multiple streams at once. Then it calls wait() at the end.

    So to sum it up:

    1. This example reads from one stream at a time and does not wait for it to finish the process.
    2. This example reads from both streams at the same time via internal threads, and waits for it to finish the process.
    3. This example waits for the process to finish, and then reads one stream at a time. And as you mentioned has the potential to deadlock if there is too much written to the streams.

    Also, you don’t need these two import statements in your 2nd and 3rd examples:

    from subprocess import communicate
    from subprocess import wait
    

    They are both methods of the Popen object.

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

Sidebar

Related Questions

First of I'm very sorry but this questions is not so so specific. All
I know it's very basic question and hope not so important, but i want
A question that I hope you can answer for a Q&A app. Still very
This is a very specific question. I hope there's someone here with good knowledge
I hope this is not a silly question, but I am very inexperienced in
I have a very specific question on CSS Specificity, something which I could not
I realize this is a very specific question, and not very helpful outside of
I know that this is a very specific question and that the behavior I
A very specific question from a novice to TDD : I separate my tests
This is a very specific question which will probably earn me the tumbleweed badge,

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.