在Python调用C函数 [英] Calling C functions in Python

查看:227
本文介绍了在Python调用C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆的,我已经写在C函数和我想要一些code,我用Python写的是能够访问这些功能。

I have a bunch of functions that I've written in C and I'd like some code I've written in Python to be able to access those functions.

我读过关于这里有一个类似的问题在这里处理(几个问题和<一href=\"http://stackoverflow.com/questions/5090585/segfault-when-trying-to-call-a-python-function-from-c\">here例如),但我感到困惑哪一种方法我需要。

I've read several questions on here that deal with a similar problem (here and here for example) but I'm confused about which approach I need to take.

一个问题建议的ctypes,另一个建议用Cython。我读过了一下这两个文件的,我完全不清楚哪一种更适合我更好。

One question recommends ctypes and another recommends cython. I've read a bit of the documentation for both, and I'm completely unclear about which one will work better for me.

基本上我已经写了一些Python code做了一些二维FFT和我想的C code至能够看到结果,然后处理它通过不同的C函数我已经书面。我不知道这是否是我更容易从C,反之亦然叫Python的。

Basically I've written some python code to do some two dimensional FFTs and I'd like the C code to be able to see that result and then process it through the various C functions I've written. I don't know if it will be easier for me to call the Python from C or vice versa.

推荐答案

如果我理解不错,你有dialoging为C => Python或类似蟒蛇=> C无preference。
在这种情况下,我会建议用Cython 。这是多种手法相当开放,特别是,在你的情况,称已经从C用Python编写的函数。

If I understand well, you have no preference for dialoging as c => python or like python => c. In that case I would recommend Cython. It is quite open to many kinds of manipulation, specially, in your case, calling a function that has been written in Python from C.

下面是它的工作原理( 公共API ):

Here is how it works (public api) :

下面的例子假设你有一个Python类(是它的一个实例),以及这个类有一个方法(名称方法,你想在这个类打电话,结果处理(在这里,)一个双击)的C.此功能,写的用Cython扩展会帮你做这个调用。

The following example assumes that you have a Python Class (self is an instance of it), and that this class has a method (name method) you want to call on this class and deal with the result (here, a double) from C. This function, written in a Cython extension would help you to do this call.

cdef public api double cy_call_func_double(object self, char* method, bint *error):
    if (hasattr(self, method)):
        error[0] = 0
        return getattr(self, method)();
    else:
        error[0] = 1

在C面,你再能像这样进行调用:

On the C side, you'll then be able to perform the call like so :

PyObject *py_obj = ....
...
if (py_obj) {
    int error;
    double result;
    result = cy_call_func_double(py_obj, (char*)"initSimulation", &error);
    cout << "Do something with the result : " << result << endl;
}

的PyObject 结构被Python / C API提供
在已经抓到了 py_obj (通过强制定期蟒蛇对象,在你用Cython扩展这样的:&LT;的PyObject *&GT; my_python_object ),你将最终能够调用就可以了 initSimulation 方法,做一些与结果。
(这里是双击,但用Cython可以用的 向量,...

Where PyObject is a struct provided by Python/C API After having caught the py_obj (by casting a regular python object, in your cython extension like this : <PyObject *>my_python_object), you would finally be able to call the initSimulation method on it and do something with the result. (Here a double, but Cython can deal easily with vectors, sets, ...)

好吧,我知道什么,我只是写可能会造成混淆,如果你从来没有使用用Cython 写任何东西,但它的目标是成为众多事物的简短演示它在长期能为你做的合并

Well, I am aware that what I just wrote can be confusing if you never wrote anything using Cython, but it aims to be a short demonstration of the numerous things it can do for you in term of merging.

通过另一只手,这种方法可能需要更多的时间比重新编写你的Python code到C,这取决于你的算法的复杂性。
在我看来,投资的时间投入到学习用Cython是相关仅如果您打算有这样的需求往往...

By another hand, this approach can take more time than recoding your Python code into C, depending on the complexity of your algorithms. In my opinion, investing time into learning Cython is pertinent only if you plan to have this kind of needs quite often...

希望这至少是信息...

Hope this was at least informative...

这篇关于在Python调用C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆