Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to exchange data between C++ and Python?

Tags:

c++

python

ipc

I'm working on a project that is written in C++ and Python. The communication between the 2 sides is made through TCP sockets. Both processes run on the same machine.

The problem is that it is too slow for the current needs. What is the fastest way to exchange information between C++ and Python?

I heard about ZeroMQ, but would it be noticeably faster than plain TCP sockets?

Edit: OS is Linux, the data that should be transferred consists of multiple floats (lets say around 100 numbers) every 0.02s, both ways. So 50 times per second, the python code sends 100 float numbers to C++, and the C++ code then responds with 100 float numbers.

like image 836
Tirafesi Avatar asked Sep 07 '25 15:09

Tirafesi


2 Answers

In case performance is the only metric you care for, shared memory is going to be the fastest way to share data between two processes running on the same machine. You can use a semaphore in shared memory for synchronization.

TCP sockets will work as well, and are probably fast enough. Since you are using Linux, I would just use pipes, it is the simplest approach, and they will outperform TCP sockets. This should get your started: http://man7.org/linux/man-pages/man2/pipe.2.html

For more background information, I recommend Advanced Programming in the UNIX Environment.

like image 83
Ton van den Heuvel Avatar answered Sep 09 '25 15:09

Ton van den Heuvel


If you're in the same machine, use a named shared memory, it's a very fast option. On python you have multiprocessing.shared_memory and in C++ you can use posix shared memory, once you're in Linux.

like image 43
João Paulo Avatar answered Sep 09 '25 13:09

João Paulo