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:
I can see a few options:
Are there other solutions I should consider?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With