C++和Python混合编程——C++调用Python入门

《C++和Python混合编程——Python调用C++入门》一文中,我们熟悉了Python调用C++编译的动态库的方法。但是作为混合编程,也必然要有反向的过程——C++调用Python代码。本文我们将介绍如何使用boost.python库实现该功能。

代码结构

初始化 Python 解释器

Py_Initialize();

在程序开始时初始化 Python 解释器,确保可以调用其他 Python C API 函数。它会设置 Python 解释器的内部状态,加载内置模块,并准备好执行 Python 代码。

获取 GIL

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

GIL(Global Interpreter Lock,全局解释器锁)是 Python 解释器中的一个机制,用于在多线程环境中保护访问 Python 对象的共享资源。GIL 确保在任何时刻只有一个线程可以执行 Python 字节码,从而避免了多线程访问共享资源时的竞争条件。

为什么需要 GIL?

Python 的内存管理不是线程安全的。为了避免多线程同时访问和修改共享数据导致的不一致性和崩溃,GIL 被引入来确保只有一个线程可以执行 Python 代码。

GIL 的影响

  • 多线程限制:由于 GIL 的存在,在 CPU 密集型任务中,多线程的性能提升有限,因为同一时刻只有一个线程在执行 Python 代码。
  • I/O 密集型任务:对于 I/O 密集型任务(如网络请求、文件读写),多线程仍然可以带来性能提升,因为 I/O 操作会释放 GIL,使其他线程有机会执行。

导入 Python 模块并执行代码

boost::python::object main_module = boost::python::import("__main__");
boost::python::object main_namespace = main_module.attr("__dict__");

std::string python_code = R"(
def add(a, b):
    return a + b

result = add(3, 4)
print(f"Result of add(3, 4) is {result}")
)";
boost::python::exec(python_code.c_str(), main_namespace);

int result = boost::python::extract<int>(main_namespace["result"]);
std::cout << "Result from Python: " << result << std::endl;

释放 GIL

PyGILState_Release(gstate);

在完成 Python 代码执行后释放 GIL。

终止 Python 解释器

Py_Finalize();

在程序结束时调用 Py_Finalize() 以清理 Python 解释器的状态,释放内存和其他资源。如果不调用 Py_Finalize(),可能会导致内存泄漏和其他资源未释放的问题。

完整代码

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <chrono>
#include <boost/python.hpp>

class PythonInterpreter {
public:
    PythonInterpreter() {
        Py_Initialize(); // Initialize Python interpreter
        gstate = PyGILState_Ensure(); // Acquire GIL
        std::cout << "Python interpreter initialized." << std::endl;
    }

    ~PythonInterpreter() {
        PyGILState_Release(gstate); // Release GIL
        Py_Finalize(); // Cleanup section
        std::cout << "Python interpreter finalized." << std::endl;
    }

private:
    PyGILState_STATE gstate;
};

void call_python_function() {
    using namespace boost::python;

    try {
        // 创建 PythonInterpreter 对象,自动初始化 Python 解释器
        PythonInterpreter pyInterp;
        
        // 导入 Python 模块
        object main_module = import("__main__");
        object main_namespace = main_module.attr("__dict__");

        // 定义并执行 Python 代码
        std::string python_code = R"(
def add(a, b):
    return a + b

result = add(3, 4)
print(f"Result of add(3, 4) is {result}")
)";
        exec(python_code.c_str(), main_namespace);

        // 获取并打印结果
        int result = extract<int>(main_namespace["result"]);
        std::cout << "Result from Python: " << result << std::endl;
    } catch (error_already_set) {
        PyErr_Print();
    }
}

int main() {
    call_python_function();
    return 0;
}

编译

以下是CMakeLists.txt的内容。

cmake_minimum_required(VERSION 3.12)

# 项目信息
# 最后一级目录为项目名称
get_filename_component(ProjectName ${CMAKE_CURRENT_SOURCE_DIR} NAME)
project(${ProjectName})

# 设置 CMP0148 政策
if(POLICY CMP0148)
    cmake_policy(SET CMP0148 NEW)
endif()

# 查找 Python 解释器和库
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)

# 查找 Boost 库。使用 Python3_VERSION_MAJOR 和 Python3_VERSION_MINOR 变量来查找对应版本的 Boost.Python 库
find_package(Boost REQUIRED COMPONENTS python${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR})

# 添加可执行文件
add_executable(${ProjectName} main.cpp)

# 包含 Python 头文件
include_directories(${Python3_INCLUDE_DIRS})

# 链接 Boost.Python 和 Python 库
target_link_libraries(${ProjectName} ${Boost_LIBRARIES} ${Python3_LIBRARIES})

执行结果

在这里插入图片描述

项目地址

https://github.com/f304646673/cpulsplus/tree/master/boost_python/c_call_p

  • 17
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

breaksoftware

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值