Pipes between Python and C++ don't get closed -
i spawning process in python using subprocess , want read output program using pipes. c++ program not seem close pipe though, when explicitly telling 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 small program 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 not return, fd not closed. opening , closing write fd in python solves problem. seems hack me. there way close fd in c++ process?
thanks lot!
spawning child process on linux (all posix oses, really) accomplished via fork
, exec
. after fork
, both processes have file open. c++ process closes it, file remains open until parent process closes fd also. normal code using fork
, , handled wrapper around fork
. read man
page pipe
. guess python has no way of knowing files being transferred child, though, , therefore doesn't know close in parent vs child process.
Comments
Post a Comment