Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing information between a python code and c++ code (IPC)

Tags:

c++

python

ipc

I have 2 code bases, one in python, one in c++. I want to share real time data between them. I am trying to evaluate which option will work best for my specific use case:

  • many small data updates from the C++ program to the python program
  • they both run on the same machine
  • reliability is important
  • low latency is nice to have

I can see a few options:

  • One process writes to a flat file, the other process reads it. It is non scalable, slow and I/O error prone.
  • One process writes to a database, the other process reads it. This makes it more scalable, slightly less error prone, but still very slow.
  • Embed my python program into the C++ one or the other way round. I rejected that solution because both code bases are reasonably complex, and I prefered to keep them separated for maintainability reasons.
  • I use some sockets in both programs, and send messages directly. This seems to be a reasonable approach, but does not leverage the fact that they are on the same machine (it will be optimized slightly by using local host as destination, but still feels cumbersome).
  • Use shared memory. So far I think this is the most satisfying solution I have found, but has the drawback of being slightly more complex to implement.

Are there other solutions I should consider?

like image 719
DevShark Avatar asked Feb 26 '16 11:02

DevShark


People also ask

How do I share data between Python and C++?

You can implement your C++ code as shared library (so or dll). Your interface should be extern "C" . Then you can call your native functions directly in python and pass your data via pointers within the same process and memory. To call the native functions you can use Python CTypes.

Can Python interact with C?

In RepTate, some theories (most notably the React application theories) are written in C code and interfaced with Python using, Ctypes. In general, already-written C code will require no modifications to be used by Python. The only work we need to do to integrate C code in Python is on Python's side.

How IPC is implemented using shared memory?

Server reads from the input file. The server writes this data in a message using either a pipe, fifo or message queue. The client reads the data from the IPC channel,again requiring the data to be copied from kernel's IPC buffer to the client's buffer. Finally the data is copied from the client's buffer.


1 Answers

First of all, this question is highly opinion-based!

The cleanest way would be to use them in the same process and get them communicate directly. The only complexity is to implement proper API and C++ -> Python calls. Drawbacks are maintainability as you noted and potentially lower robustness (both crash together, not a problem in most cases) and lower flexibility (are you sure you'll never need to run them on different machines?). Extensibility is the best as it's very simple to add more communication or to change existing. You can reconsider maintainability point. Can you python app be used w/o C++ counterpart? If not I wouldn't worry about maintainability so much.

Then shared memory is the next choice with better maintainability but same other drawbacks. Extensibility is a little bit worse but still not so bad. It can be complicated, I don't know Python support for shared memory operation, for C++ you can have a look at Boost.Interprocess. The main question I'd check first is synchronisation between processes.

Then, network communication. Lots of choices here, from the simplest possible binary protocol implemented on socket level to higher-level options mentioned in comments. It depends how complex your C++ <-> Python communication is and can be in the future. This approach can be more complicated to implement, can require 3rd-party libraries but once done it's extensible and flexible. Usually 3rd-party libraries are based on code generation (Thrift, Protobuf) that doesn't simplify your build process.

I wouldn't seriously consider file system or database for this case.

like image 182
Andriy Tylychko Avatar answered Sep 20 '22 08:09

Andriy Tylychko