1

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?

2
  • btw data is already plural Commented Dec 11, 2017 at 14:02
  • That's right. I'm going to correct it. Thanks and sorry for my english ^^' Commented Dec 11, 2017 at 14:16

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I read somewhere that the limit or arguments was often 4096. But, for example, if I know that I will always have like 1000 arguments, is that a reason to parse the command line each time?
AFAIK the limit is in bytes, not in number of arguments. So if you have 100 arguments, 4096 gives you ~ 39 bytes per argument (assuming 1 space as separator, and calculating using (4096-99)/100, not taking into account the command length itself).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.