所以我尝试使用cffi在pypy中快速访问c库。在
我正在使用我的macpro命令行工具9.1
具体来说,我比较的是纯python优先级队列、heapq、cffi和ctypes。在
我从roman10的网站上得到了优先队列的C实现代码。在
我正在遵循cffi的文档,但是当我试图调用库内部的函数时遇到了一个错误。在
我有5个文件:优先级队列的.c/.hcheap.c
cheap.h
,具有cffiheap_inter.py
的接口python文件,访问队列的程序的包装器heap.py
,以及一个测试脚本test_heap.py
这是错误:我尝试使用export CC=gcc
和{
pypy test_heap.py
AttributeError:cffi库''u heap'i'没有名为'initQueue'的函数、常量或全局变量
不过,我在c库中有一个正在编译的函数initQueue。在
当我叫pypy在堆上_内部py执行的命令是
building '_heap_i' extension
clang -DNDEBUG -O2 -fPIC -I/opt/local/include -I/opt/local/lib/pypy/include -c _heap_i.c -o ./_heap_i.o
cc -pthread -shared -undefined dynamic_lookup ./_heap_i.o -L/opt/local/lib -o ./_heap_i.pypy-41.so
clang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]
这是我的消息来源
cheap.h
#include <stdio.h>
#include <stdlib.h>
/* priority Queue implimentation via roman10.net */
struct heapData {
//everything from event
//tx string
char tx[64];
//txID int
int txID;
//rx string
char rx[64];
//rxID int
int rxID;
//name string
char name[64];
//data Object
//time float
float time;
};
struct heapNode {
int value;
struct heapData data; //dummy
};
struct PQ {
struct heapNode* heap;
int size;
};
void insert(struct heapNode aNode, struct heapNode* heap, int size);
void shiftdown(struct heapNode* heap, int size, int idx);
struct heapNode removeMin(struct heapNode* heap, int size);
void enqueue(struct heapNode node, struct PQ *q);
struct heapNode dequeue(struct PQ *q);
struct heapNode peak(struct PQ *q);
void initQueue(struct PQ *q, int n);
int nn = 1000000;
struct PQ q;
int main(int argc, char **argv);
然后heap.c
heap_inter.py
:
from cffi import FFI
ffibuilder = FFI()
ffibuilder.set_source("_heap_i",
r"""//passed to C compiler
#include <stdio.h>
#include <stdlib.h>
#include "cheap.h"
""",
libraries=[])
ffibuilder.cdef("""
struct heapData {
char tx[64];
int txID;
char rx[64];
int rxID;
char name[64];
float time;
} ;
struct heapNode {
int value;
struct heapData data; //dummy
} ;
struct PQ {
struct heapNode* heap;
int size;
} ;
""")
#
# Rank (Simian Engine) has a Bin heap
if __name__ == "__main__":
ffibuilder.compile(verbose=True)
然后heap.py
from _heap_i import ffi, lib
infTime = 0
# /* create heap */
def init():
#global infTime
#infTime = int(engine.infTime) + 1
cheap = ffi.new("struct PQ *")
lib.initQueue(cheap,nn)
return cheap
def push(arr, element):
hn = ffi.new("struct heapNode")
value = element["time"]
hn.value = value
rx = ffi.new("char[]", element["rx"])
hn.data.rx = rx
tx = ffi.new("char[]", element["tx"])
hn.data.tx = tx
txID = ffi.new("int", element["txID"])
hn.data.txID = txID
rxID = ffi.new("int", element["rxID"])
hn.data.rxID = rxID
name = ffi.new("int", element["name"])
hn.data.name = name
hn.data.time = value
result = lib.enqueue(hn, arr)
def pop(arr):
hn = lib.dequeue(arr)
element = {"time": hn.value,
"rx" : hn.data.rx,
"tx" : hn.data.tx,
"rxID" : hn.data.rxID,
"txID" : hn.data.txID,
"name" : hn.data.name,
"data" : hn.data.data,
}
return element
def annihilate(arr, event):
pass
def peak(arr):
hn = lib.peak(arr)
element = {"time": hn.value,
"rx" : hn.data.rx,
"tx" : hn.data.tx,
"rxID" : hn.data.rxID,
"txID" : hn.data.txID,
"name" : hn.data.name,
"data" : hn.data.data,
}
return element
def isEvent(arr):
if arr.size:
return 1
else:
return None
def size(arr):
return arr.size
最后测试_堆.py在
import heap
pq = heap.init()
for x in range(1000) :
heap.push({"time" : x,
"rx" : "a",
"tx" : "b",
"txID" : 1,
"rxID" : 1,
"name" : "bob",
"data": "none",
},pq)
for x in range(100) :
heap.peak(pq)
for x in range(1000):
y = heap.dequeue(pq)
print y
谢谢大家的关注,如果你以前有过这样的经历,请告诉我。在
你的外国金融机构()没有任何函数声明,因此生成的\u heap i库不导出任何函数。尝试从.h文件复制粘贴函数声明。在
你必须告诉我ffi.set_源()关于
heap.c
。最简单的方法是with open('heap.c', 'r') as fid: ffi.set_source(fid.read())
比如this example。它的工作方式是:
distutils
关键字)相关问题 更多 >
编程相关推荐