简体   繁体   English

将结构从python代码传递到C库

[英]pass a structure from python code to a C library

I have a C library in which I defined a tree structure: 我有一个C库,其中定义了一个树结构:

struct node {
    int * info;
    struct node *left;
   struct node *right;
}

I want to make a call of C functions from python. 我想从python调用C函数。

I wish to know what possibilities do I have to reconstruct the tree in python, that is built inside the C code using the quoted structure? 我想知道在python中使用引用结构重建在C代码内部构建的树有什么可能性?

I intend to use ctypes . 我打算使用ctypes Do I have a better module for this? 我为此有更好的模块吗?

You might check out cffi. 您可以签出cffi。 It's kinda like ctypes, except it's more API-level than ABI-level. 它有点像ctypes,除了它的API级别比ABI级别高。 ctypes is ABI-level. ctypes是ABI级别的。 It's also quite a bit younger than ctypes, and isn't yet included with CPython. 它也比ctypes年轻很多,并且尚未包含在CPython中。

You could also try Cython, which allows you to pretty freely intermix C symbols and Python symbols using a Python-like syntax. 您也可以尝试Cython,它可以让您使用类似Python的语法自由地混合C符号和Python符号。 You can use Cython to generate a .c file from a .pyx, where .pyx is quite a bit like .py. 您可以使用Cython从.pyx生成.c文件,其中.pyx很像.py。 Of course, you then create a .so from your .c and import it into CPython. 当然,然后您可以从.c创建一个.so并将其导入到CPython中。 I've used m4 to create pure python and cython from the same file - it worked well. 我已经使用m4从同一个文件创建纯python和cython-效果很好。

Apache Thrift might be a better fit for you, which is a binary communication protocol developed by Facebook for various common programming languages including C++, Java, Python, Ruby ... etc (here is its wiki page for more details). Apache Thrift可能更适合您,它是Facebook针对各种常见编程语言(包括C ++,Java,Python,Ruby ...等)开发的二进制通信协议(更多信息,请访问其Wiki页面 )。

You might also interested in the following question asked in Stackoverflow, Thrift client-server multiple roles , which includes how the client calls a function in the server. 您可能还对Stackoverflow中提出的以下问题感兴趣: Thrift客户端-服务器的多个角色 ,其中包括客户端如何在服务器中调用函数。

EDIT: As thrift only supports common primitive types, struct, and three container types (list<t1>, set<t1>, map<t1, t2>), you need to work with pointers. 编辑:由于节俭只支持常见的基本类型,结构和三种容器类型(列表<t1>,set <t1>,map <t1,t2>),因此您需要使用指针。 One way to work with is to use id to instance map. 一种处理方式是使用id来实例映射。 For example, you can assign an unique id for each node and use id to refer to the left and right node, and have an int to node map to fetch nodes by ids. 例如,您可以为每个节点分配一个唯一的ID,并使用ID来指代左右节点,并使用一个int到节点的映射来通过id来获取节点。 Here is how the .thrift file of your node struct might look like: 节点结构的.thrift文件如下所示:

struct Node {
  // a thrift struct is composed of fields;
  // each field has a unique integer identifier, a type, a name and an optional default value.
  1: required i32 nodeId;
  2: required i32 infoId;
  3: required i32 leftNodeId;
  4: required i32 rightNodeId;
}

For the function(s) you written in C and would like to invoke from python, you need to pack them into a thrift service. 对于您用C编写并想从python调用的函数,您需要将它们打包到一个节俭服务中。 Here is the service of your function might look like (sorry, I didn't know your function interface at the time when I was editing this answer): 这是您的函数的服务可能看起来像(抱歉,我在编辑此答案时不知道您的函数接口):

service TreeStructureService {
  void processTreeNode(1: Node node);
}

If you would like to learn more about thrift, you might find Thrift: The Missing Guide useful. 如果您想了解有关Thrift的更多信息,可能会发现Thrift:The Missing Guide有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM