0

A python program opens a new process of the C++ program and is reading the processes stdout. No problem so far.

But is it possible to have multiple streams like this for communication? I can get two if I misuse stderr too, but not more. Easy way to hack this would be using temporary files. Is there something more elegant that does not need a detour to the filesystem?

PS: *nix specific solutions are welcome too

Dennis Kempin
  • 1,138
  • 2
  • 13
  • 19

4 Answers4

2

On unix systems; the usual way to open a subprocess is with fork(), which will leave any open file descriptors (small integers representing open files or sockets) available in both the child, and the parent, and then exec(), which also allows the new executable to use the file descriptors that were open in the old process. This functionality is preserved in the subprocess.Popen() call (adjustable with the close_fds argument). Thus, what you probably want to do is use os.pipe() to create pairs of sockets to communicate on, then use Popen() to launch the other process, with arguments for each of fd's returned by the previous call to pipe() to tell it which fd's it should use.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • If you go with this approach, you may want to look at [this question](http://stackoverflow.com/questions/2746168/how-to-construct-a-c-fstream-from-a-posix-file-descriptor) for how to deal with the fd's on the C++ side. – SingleNegationElimination Jun 22 '12 at 21:59
0

Sounds like what you want are to use sockets for communication. Both languages let open raw sockets but you might want to check out the zeromq project as well which has some addition advantages for message passing. Check out their hello world in c++ and python.

Trevor
  • 9,518
  • 2
  • 25
  • 26
0

assuming windows machine. you could try using the clipboard for exchanging information between python processes and C++.

assign some unique process id followed by your information and write it to clipboard on python side.....now just parse the string on C++ side. its akin to using temporary files but all done in memory..... but the drawback being you cannot use clipboard for any other application. hope it helps

Tanu
  • 237
  • 2
  • 7
0

With traditional, synchronous programming and the standard Python library, what you're asking is difficult to accomplish. If, instead, you consider using an asynchronous programming model and the Twisted library, it's a piece of cake. The Using Processes HOWTO describes how to easily communicate with as many processes as you like. Admittedly, there's a bit of a learning curve to Twisted but it's well worth the effort.

Rakis
  • 7,779
  • 24
  • 25