相关疑难解决方法(0)

Boost.Python:Wrap函数释放GIL

我目前正在使用Boost.Python,并希望得到一些帮助来解决一个棘手的问题.

上下文

当C++方法/函数暴露给Python时,它需要释放GIL(全局解释器锁)以让其他线程使用解释器.这样,当python代码调用C++函数时,解释器可以被其他线程使用.现在,每个C++函数看起来像这样:

// module.cpp
int myfunction(std::string question)
{
    ReleaseGIL unlockGIL;
    return 42;
}
Run Code Online (Sandbox Code Playgroud)

为了传递它来提升python,我做:

// python_exposure.cpp
BOOST_PYTHON_MODULE(PythonModule)
{
    def("myfunction", &myfunction);
}
Run Code Online (Sandbox Code Playgroud)

问题

这个方案工作正常,但它暗示这module.cpp取决于Boost.Python没有充分理由.理想情况下,只python_exposure.cpp应该依赖Boost.Python.

解?

我的想法是用Boost.Function这样包装函数调用:

// python_exposure.cpp
BOOST_PYTHON_MODULE(PythonModule)
{
    def("myfunction", wrap(&myfunction));
}
Run Code Online (Sandbox Code Playgroud)

这里wrap将负责在通话期间解锁GIL myfunction.这种方法的问题是wrap需要具有相同的签名,myfunction这几乎意味着重新实现Boost.Function...

如果有人对此问题有任何建议,我将非常感激.

c++ python boost function boost-python

9
推荐指数
1
解决办法
3114
查看次数

标签 统计

boost ×1

boost-python ×1

c++ ×1

function ×1

python ×1