fastest way to share data between a C++ and Python program?

If you are using CPython (the most common implementation of python) then you can create a dynamic library that can be used as a python module. There Boost.Python

Which can be used as:

#include <boost/python.hpp>
char const* greet()
{
   return "hello, world";
}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}
> import hello_ext   
> print(hello_ext.greet())
hello, world

To build with python 3.7 and boost 1.68.0 you can use following CMake file

cmake_minimum_required(VERSION 3.9.0 FATAL_ERROR)

project("boost_python_sample" LANGUAGES CXX)

set(BOOST_ROOT "C:/local/boost_1_68_0")
find_package(Boost REQUIRED COMPONENTS python37)
set(Python3_ROOT_DIR "C:/python37")
find_package(Python3 REQUIRED COMPONENTS Development)

add_library("boost_python_sample" SHARED "main.cpp")
target_link_libraries("boost_python_sample" Boost::python37 Python3::Python)
target_compile_definitions("boost_python_sample" PUBLIC "BOOST_PYTHON_STATIC_LIB")

One way to exchange data between python and C++ is to use a message queue library. One possible library which is designed to be fast is ØMQ (zeroMQ). Is this the fastest way? It depends on your use case. It might be worth evaluating. Especially considering the easy to implement part, good documentation and community support.


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.

Tags:

Python

C++

C++11