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

  • Home
  • SEARCH
  • 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 6025825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:21:23+00:00 2026-05-23T04:21:23+00:00

I have 5 processes p1,p2,…,p5 where I want to write some data to stdin

  • 0

I have 5 processes p1,p2,...,p5 where I want to write some data to stdin of p1, pipe p1 output to p2 stdin and finally read the final result from output of p5.

What I have tried so far:

p1 = Popen(['p1'], stdin=PIPE, stdout=PIPE)
p2 = Popen(['p2'], stdin=p1.stdout, stdout=PIPE)
...
p5 = Popen(['p5'], stdin=p4.stdout, stdout=PIPE)

# write data to stdin
p1.stdin.write(indata)
p1.stdin.close()

# not sure in what order to close the pipes here, if at all

# read output
out = p5.stdout.read()
print out

The last code snipped just hangs, because I must be doing the read/write operations incorrectly.

I was able to get working single processes using communicate() and two processes without suppling any input to first one (example from Python docs):

output=`dmesg | grep hda`
==>
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

But I cannot figure out, how to supply input to first process without hanging the interpreter.

I could also use a bash script for that (that I’ve already written and works), but I’d like to know, how to achieve same with Python.

So, I’d like to ask, how do do all that properly, specifically in what order to do the read/write/close operations on pipes?

I’m working on 64-bit Linux, if that matters.

EDIT: I forgot to mention that all processes p1,..p5 consume all input they are given, process it, write to stdout and then terminate. Thus, the processes next in pipeline should not terminate before the previous ones have finished processing.

EDIT2: I know that I could also use

command = 'bash -c "p1 | p2 | p3 | p4 | p5"'
proc = Popen([command], shell=True)
out, err = proc.communicate(input=indata)
print out

but my main interest is getting know how to chain the pipes purely in python code.

  • 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-23T04:21:24+00:00Added an answer on May 23, 2026 at 4:21 am

    Maybe this can help:

    import sys
    import tempfile
    from subprocess import Popen, PIPE
    
    
    cmd = [sys.executable, '-c', 'print raw_input()']
    
    # Using a temp file to give input data to the subprocess instead of stdin.write to avoid deadlocks.
    with tempfile.TemporaryFile() as f:
        f.write('foobar')
        f.seek(0)  # Return at the start of the file so that the subprocess p1 can read what we wrote.
        p1 = Popen(cmd, stdin=f, stdout=PIPE)
    
    p2 = Popen(cmd, stdin=p1.stdout, stdout=PIPE)
    p3 = Popen(cmd, stdin=p2.stdout, stdout=PIPE)
    
    # No order needed.
    p1.stdout.close()
    p2.stdout.close()
    
    # Using communicate() instead of stdout.read to avoid deadlocks. 
    print p3.communicate()[0]
    

    Output:

    $ python test.py
    foobar
    

    Hope this can be hepfull.

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

Sidebar

Related Questions

I have multiple processes monitoring an MSMQ queue. I want to do multi-step operations
I have two processes one will query other for data.There will be huge amount
I have several server processes that once in a while respond to messages from
I have two running processes in Windows, and each process has a pipe to
I have two C++ processes (A and B), executing under Windows, where one launches
I have two unrelated processes that use .NET assemblies as plugins. However, either process
I have an application where I have potentially many processes must send a message
A single Biztalk Server can have multiple Host processes. Is it possible to create
I think the question says it all. I have several monthly processes in stored
I have a class that processes a 2 xml files and produces a text

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.