如何使用 pybind11 将 python 函数保存到静态 c++ 容器中?

How can I save a python function to a static c++ container using pybind11?

本文关键字:c++ 静态 保存 pybind11 何使用 python 函数      更新时间:2023-10-16

本质上,在C++端,我有一个包含某种类型函数的容器。现在我想向 python 公开这个容器,以便用户提供自己的 python 函数。

最简单的示例如下所示:

#include "pybind/common/Common.h"
using CppFunc = std::function< int (int) >;
PYBIND11_MODULE( test, m )
{
m.def("addFunc", [](const pybind11::function& f){
static std::vector<CppFunc> vec{};
vec.push_back(f.cast<CppFunc>());
});
}

然后在python中,我想做这样的事情。

import test
def myFunc(number):
return number+1
test.addFunc(myFunc)

有趣的是,这工作正常。但是,如果我使用"python script.py"运行脚本,它会运行,然后永远不会终止。在交互式控制台中,相同的代码可以正常工作,直到您尝试关闭控制台:进程卡住。

如何安全地将此 python 函数存储在C++容器中?

>static std::vector<CppFunc> vec{}存储对python对象(用户函数(的引用,这些对象由于静态存储而永远不会被释放,因此解释器不能终止。

为了确保解释器终止,您可以在模块终止时调用清理函数:

#include "pybind11/pybind11.h"
#include "pybind11/functional.h"
namespace py = pybind11;
using CppFunc = std::function< int (int) >;
PYBIND11_MODULE( test , m )
{
static std::vector<CppFunc> vec{};
m.def("addFunc", [](CppFunc f){
vec.push_back(std::move(f));
});
m.add_object("_cleanup", py::capsule([]{ vec.clear(); }));
}

有关更多详细信息,请参阅文档:https://pybind11.readthedocs.io/en/stable/advanced/misc.html#module-destructors