4

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.

14
  • Why not using UDP? or Shared memory?
    – masoud
    Commented Sep 30, 2019 at 13:58
  • 3
    How about pipes? And what is the data you need to transfer? Is it one-way only? What are the requirements you have for efficiency? What is "too slow"? Commented Sep 30, 2019 at 14:04
  • SWIG to call C++ in Python Commented Sep 30, 2019 at 14:04
  • 1
    If you are only transferring 400 bytes or 800 bytes, 50 times per second, this should not require special solutions and you should be able to get that level of performance even without any special optimizations. What performance numbers are you getting? Commented Sep 30, 2019 at 14:15
  • 1
    800 bytes, 50 times per second is only ~40KB/s or 320Kb/s. Any TCP connection should be able to handle that. Commented Sep 30, 2019 at 14:16

3 Answers 3

2

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.

1
  • 1
    Pipes have better performance than TCP (or in other words less overhead).
    – nada
    Commented Sep 30, 2019 at 14:17
2

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.

1

Short answer, no, but ZeroMQ can have other advantages. Lets go straight for it, if you are on Linux and want fast data transfer, you go Shared memory. But it will not as easy as with ZeroMQ.

Because ZeroMQ is a message queue. It solves (and solves well) different problems. It is able to use IPC between C++ and Python, what can be noticeably faster than using sockets (for the same usages), and gives you a window for network features in your future developments. It is reliable and quite easy to use, with the same API in Python and C++. It is often used with Protobuf to serialize and send data, even for high troughput.

The first problem with IPC on ZeroMQ is that it lacks Windows support, because it is not a POSIX compliant system. But the biggest problem is maybe not there: ZeroMQ is slow because it embeds your message. You can enjoy the benefits of it, but it can impede performances. The best way to check this is, as always, to test it by yourself with IPC-BENCH as I am not sure the benchmark I provided in the previous link was using the IPC. The average gain with IPC against local domain TCP is not fantastic though.

As I previously said, I am quite sure Shared Memory will be the fastest, excluding the last possibility: develop your own C++ wrapper in Python. I would bet that is the fastest solution, but will require a bit of engineering to multithread if needed because both C++ and Python will run on the same process. And of course you need to adjust the current C++ code if it is already started.

And as usual, remember that optimization always happen in a context. If data transfer is only a fraction of the running time when compared to the processing you can do after, or if you can wait the 0.00001sec that using Shared memory would help you to gain, it might be worth it to go directly to ZeroMQ because it will be simpler, more scalable and more reliable.

1
  • Hi, I believe zmq_ipc is slow because under the hook, it is not using the shared memory but Transparent Inter-Process Communication (TIPC). Embedding another message shouldn't make it significantly slower...
    – HCSF
    Commented Jul 25, 2021 at 8:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.