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

大纲

  • 代码结构
    • [初始化 Python 解释器](#初始化 Python 解释器)
    • [获取 GIL](#获取 GIL)
      • [为什么需要 GIL?](#为什么需要 GIL?)
      • [GIL 的影响](#GIL 的影响)
    • [导入 Python 模块并执行代码](#导入 Python 模块并执行代码)
    • [释放 GIL](#释放 GIL)
    • [终止 Python 解释器](#终止 Python 解释器)
  • 完整代码
  • 编译
  • 执行结果
  • 项目地址

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

代码结构

初始化 Python 解释器

cpp 复制代码
Py_Initialize();

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

获取 GIL

cpp 复制代码
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 模块并执行代码

cpp 复制代码
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

cpp 复制代码
PyGILState_Release(gstate);

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

终止 Python 解释器

cpp 复制代码
Py_Finalize();

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

完整代码

cpp 复制代码
#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的内容。

bash 复制代码
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

相关推荐
葬爱家族小阿杰6 分钟前
python执行测试用例,allure报乱码且未成功生成报告
开发语言·python·测试用例
xx155802862xx8 分钟前
Python如何给视频添加音频和字幕
java·python·音视频
酷爱码8 分钟前
Python实现简单音频数据压缩与解压算法
开发语言·python
花果山总钻风35 分钟前
SQLAlchemy 中的 func 函数使用指南
python
豪斯有话说1 小时前
C++_哈希表
数据结构·c++·散列表
知识中的海王1 小时前
Python html 库用法详解
开发语言·python
面朝大海,春不暖,花不开1 小时前
使用 Python 正则表达式实现文本替换与电话号码规范化
python·mysql·正则表达式
淘小白_TXB21961 小时前
Python网页自动化Selenium中文文档
python·selenium·自动化·网页自动化
Clair-Sean1 小时前
【JavaSE】多线程基础学习笔记
笔记·python·学习
real_metrix2 小时前
【学习笔记】erase 删除顺序迭代器后迭代器失效的解决方案
c++·迭代器·迭代器失效·erase