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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:47:14+00:00 2026-06-07T03:47:14+00:00

I am spawning a process in python using subprocess and want to read output

  • 0

I am spawning a process in python using subprocess and want to read output from the program using pipes. The C++ program does not seem to close the pipe though, even when explicitly telling it to close.

#include <cstdlib>
#include <ext/stdio_filebuf.h>
#include <iostream>

int main(int argc, char **argv) {
  int fd = atoi(argv[1]);
  __gnu_cxx::stdio_filebuf<char> buffer(fd, std::ios::out);
  std::ostream stream(&buffer);
  stream << "Hello World" << std::endl;
  buffer.close();
  return 0;
}

I invoke this small program with this python snippet:

import os                                                                                         
import subprocess                                                                                 

read, write = os.pipe()                                                                           
proc = subprocess.Popen(["./dummy", str(write)])                                                  
data = os.fdopen(read, "r").read()                                                                
print data                                                                                        

The read() method does not return, as the fd is not closed. Opening and closing the write fd in python solves the problem. But it seems like a hack to me. Is there a way to close the fd in my C++ process?

Thanks a lot!

  • 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-07T03:47:15+00:00Added an answer on June 7, 2026 at 3:47 am

    Spawning a child process on Linux (all POSIX OSes, really) is usually accomplished via fork and exec. After fork, both processes have the file open. The C++ process closes it, but the file remains open until the parent process closes the fd also. This is normal for code using fork, and usually is handled by a wrapper around fork. Read the man page for pipe. I guess python has no way of knowing which files are being transferred to the child, though, and therefore doesn’t know what to close in the parent vs the child process.

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

Sidebar

Related Questions

I'm spawning a process from Win32 using CreateProcess , setting the hStdOutput and hStdError
From within my master python program, I am spawning a child program with this
I have a program spawning and communicating with CPU heavy, unstable processes, not created
I'm spawning an ssh process in my python script like this: env = {'SSH_ASKPASS':'/home/max/repo/vssh/vssh/vssh.py',
What is the method of getting the return value from spawning a sub-process within
I want to spawn a process using spawn_async in the GLib bindings in javascript
http://pypi.python.org/pypi/Spawning/ So I'm playing with Spawning... If I run it with 10 process and
I'd like to control a bash program from within Python. I'd like to run
How would I go about spawning a separate process using a SQL Server 05/08
I've had a bunch of EAGAIN's from trying to fork() or spawning threads, which

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.