Sending data from python to c++ as parameters or file?

Question:

I have two programs. My main program is in python and I have another one in c++ because I have to connect to a machine and the only way I can use this machine is a dll/lib made with c++ that the constructor gave me.

In my python, I’m getting from a database thousands points and I’m doing some operations on them and those points are stocked in differents array in a class.

Now, I need to send thoses points to the c++ program and I was wondering how. I can send them as parameters to the main of the c++ program but the command line is then too long and I think even if it wasn’t too long, this would be a really bad practice (If someone can explain a little why, that would be cool!).

Actually, I’m thinkinng to generate a file with all the coordonates in this file in my python program and send it too the c++ but I have to parse it then in c++.
Is it a good way to do it? Or maybe an other solution would be better?

Asked By: Stephie

||

Answers:

Some ways:

  1. Use subprocess.Popen() with stdin=subprocess.PIPE and write the data one per line, parsing it on the C++ side
  2. Use temporary files, either text files or other files
  3. Use ctypes and load the C++ part as shared library and then call into the C++ functions from Python directly
  4. Use Python’s struct module to encode your data from Python into C structs, which avoids the string formatting and string parsing — this can be combined with any of the three above: subprocess.Popen (read binary data from stdin in C++), temporary files (open in binary mode) or ctypes (pass a pointer to the struct directly to the library function)

As to the question why passing arguments on the command line is problematic, there are limits imposed by the OS, e.g. getconf ARG_MAX.

Answered By: Thomas Perl
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.