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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:48:01+00:00 2026-06-04T00:48:01+00:00

hello i have question in Python subprocess module I want to interaction with child

  • 0

hello i have question in Python subprocess module

I want to interaction with child process (ex: child’s stdin, stdout)

This is child Program’s code

  1 #include <stdio.h>
  2 
  3 int main(){
  4         char buf[1024];
  5         printf("asdfsadfasff\n");
  6         scanf("%s",buf);
  7         printf("%s\n",buf);
  8 }

this code is very simple, print the string, wait input, and print input string

and next, the python code is below

  1 #!/usr/bin/python
  2 from subprocess import *
  3 
  4 fd = Popen(["./random"],stdin=PIPE,stdout=PIPE)
  5 print "pid = %d"%(fd.pid) # print pid
  6 result = ""
  7 result = fd.stdout.read(-1) # read
  8 print result
  9 fd.stdin.write("write write write!!!\n")
 10 result = fd.stdout.read(-1)
 11 print result

in this case, I expected this program will work well, but in line 7 (fd.stdout.read(-1) was blocked and never working

I changed read function’s param (read(-1), read(1), read(), read(1024)) but everything do not work

But, when i give string in stdin first, program worked.

I think child’s stdout buffer was not fill when the program is ended… this is just my opnion

Is there any solution about this problem?

Edit1.
when I execute program which is print string first, and wait user’s input such as “sudo su”
This worked well that I expected

I cannot understand why this case work well

  1 from subprocess import *
  2 
  3 fd = Popen(["sudo","su"],stdin=PIPE,stdout=PIPE)
  4 
  5 print fd.stdout.read(-1)
  6 print fd.stdin.write("asdfasdfas\n")
  • 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-04T00:48:03+00:00Added an answer on June 4, 2026 at 12:48 am

    The child process ./random is using stdio, which by default does “full buffering” rather than “line buffering” when the process’s stdout is not a tty device. Since python runs the process via pipes (which are not tty devices) the program is buffering its output, and not printing it at the time of the scanf call. The buffer is flushed (written to stdout) only (a) when it fills up; (b) when explicitly flushed with an fflush call; or (c) at program exit.

    You can modify the child process to do explicit fflush calls, or you can use a pseudo-tty from Python, e.g., with pexpect or sh. (Note that the sh module by default uses a pty for output only but this is usually sufficient.) Or, in cases like this where the program you’re running only “looks interactive” (but is actually totally predictable), you can just run it with the known input sequence pre-provided:

    fd = Popen(["./random"], stdin=PIPE, stdout=PIPE)
    result = fd.communicate("write write write!!!\n")[0]
    

    although then of course you’ll get all the stdout input mixed together, and have to separate out the two lines it printed (in this particular case).

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

Sidebar

Related Questions

Hello this is may first question and I have found so far the following
i have this: <div id=parent style=overflow:auto> ... <div id=test>Hello</div> ... </div> My question is:
Hello I am new to python and have a question about the best/pythonic way
I have used this question to learn install my python script as a service
I have been going crazy over a seemingly easy question with Python: I want
Hello i have a question on picking random entries from a database. I have
Hello is have a question for a school assignment i need to : Read
hello I have a small question in objective-C memory management. I know that if
Hello guys sorry if the question looks stupid to you. i have 3 tables
I have the following regexp: (?P<question>.+(?<!\[\[)) It is designed to match hello world! in

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.