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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:19:51+00:00 2026-06-14T17:19:51+00:00

The codes are like this: from subprocess import Popen, PIPE p1 = Popen(command1, stdout

  • 0

The codes are like this:

from subprocess import Popen, PIPE


p1 = Popen("command1", stdout = PIPE)
p2 = Popen("command2", stdin = p1.stdout, stdout = PIPE)
result_a = p2.communicate()[0]

p1_again = Popen("command1", stdout = PIPE)
p3 = Popen("command3", stdin = p1_again.stdout, stdout = PIPE)
result_b = p3.communicate()[0]

with open("test") as tf:
    p1_again_again = Popen("command1", stdout = tf)
    p1_again_again.communicate()

The bad part is:

The command1 was executed three times because when I use commnnicate once, the stdout of that Popen object can’t be used again. I was just wondering whether there’s a method to reuse the intermediate results of PIPE.

Does anyone have ideas about how to make these codes better (better performance as well as less lines of codes)? Thanks!

  • 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-14T17:19:52+00:00Added an answer on June 14, 2026 at 5:19 pm

    here is a working solution. I have put example commands for cmd1, cmd2, cmd3 so that you can run it. It just takes the output from the first command and uppercases it in one command and lowercases it in the other.

    code

    from subprocess import Popen, PIPE, check_output
    from tempfile import TemporaryFile
    
    cmd1 = ['echo', 'Hi']
    cmd2 = ['tr', '[:lower:]', '[:upper:]']
    cmd3 = ['tr', '[:upper:]', '[:lower:]']
    
    with TemporaryFile() as f:
        p = Popen(cmd1, stdout=f)
        ret_code = p.wait()
        f.flush()
        f.seek(0)
        out2 = Popen(cmd2, stdin=f, stdout=PIPE).stdout.read()
        f.seek(0)
        out3 = Popen(cmd3, stdin=f, stdout=PIPE).stdout.read()
        print out2, out3
    

    output

    HI
    hi
    

    some of the things to make note of in the solution. the tempfile module is always a great way to go when needing to work with temp files, it will automatically delete the temporary file as a cleanup once the with statement exits, even if there was some io exception thrown through out the with block. cmd1 is run once and output to the temp file, one calls the wait() method to make sure all execution has completed, then we do seek(0) each time so that when we call the read() method on f it is back at the start of the file. As a reference the question Saving stdout from subprocess.Popen to file, helped me in getting the first part of the solution.

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

Sidebar

Related Questions

i've got this snazzy python code: import subprocess value = subprocess.Popen([php,./php/php_runner.php],stdout=subprocess.PIPE); the problem is,
I have this code here import subprocess from time import strftime # Load just
See this simple code: $ python >>> from subprocess import * >>> call(['echo','Hi']) Hi
I saw a piece of code which was written like this: if (from n
I have several HTML code combinations like this: <h2></h2> <p></p> <iframe></iframe> <hr/> and from
I'm automating Visio 2003 from a VB.NET app. My code looks like this (with
The Problem I'm receiving crash reports from users that look like this: Code Type:
I would like do some thing like this: SELECT sum(quantity) FROM orders WHERE order_code
I have this source code from 2001 that I would like to compile. It
I'd like to call a Clojure function from Java code. This question hasn't been

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.