问题描述
我有一个 C++ 库,它有一个 Python 包装器(用 SWIG 编写).该库允许执行小的用户定义代码(回调),例如向量上的元素操作.IE.你可以做任何任意的二进制函数,而不仅仅是一个 +.现在,这是通过接受二进制函数的可调用 Python 对象并调用它来完成的.它可以工作,但比不必在每次迭代中上下反弹到 Python 的代码慢约 80 倍.
I have a C++ library that has a Python wrapper (written with SWIG). This library allows executing small user-defined code (a callback), such as element-wise operations on a vector. I.e. instead of just a + you can do whatever arbitrary binary function. Right now this is accomplished by accepting a callable Python object for the binary function and calling it. It works, but is about 80 times slower than code that doesn't have to bounce up and down into Python at every iteration.
我如何编写/构建/导入 Cython 函数并将其传递到我的 C++ 库中,以便它可以被 C++ 库直接调用?
How would I write/build/import a Cython function could be passed into my C++ library so that it can be called directly by the C++ library?
如果我只是坚持使用 C,那么我会写一些类似
If I just stuck to C then I would write something like
EWise(double (*callback)(double, double))
EWise 然后会 callback(10, 20);
等等.我希望 callback
用 Cython 编写,使用用户想要的任何名称,并且必须以某种方式通过 Python 将指向它的指针传递给我的 C++ 库.这就是我不清楚的地方.
EWise would then callback(10, 20);
or such. I want callback
to be written in Cython, using whatever name the user wants, and a pointer to it has to be passed to my C++ library through Python somehow. That somehow is where I'm unclear.
推荐答案
cython 的诀窍在于使用关键字 public
The trick with cython is in using the keyword public
cdef public double cython_function( double value, double value2 ):
return value + value2
然后命令 cythonize
和
将创建标题
> 您可以包括在内.或者,您可以自己创建标题:
Then the command cythonize <your_file.pyx>
along with <your_file.c>
will create header <your_file.h>
that you can include.
Alternatively, you can create the header yourself:
#ifdef __cplusplus {
extern "C"
#endif
double cython_function( double value, double value2 );
#ifdef __cplusplus
}
#endif
更新:
然后通过 Python 的一些覆盖,您可以使用 ctypes 的回调机制
Then with a little overlay from Python you can use ctypes's callback mechanism
func_type = CFUNCTYPE(c_double, c_double, c_double)
your_library.set_callback_function ( func_type(user_modules.cython_function) )
这篇关于从 C++ 调用 Cython 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!