相互調(diào)用的方式選擇
在做項目時雀彼,調(diào)研過兩種方式:一是擴展Python ctypes 類型壤蚜;二是引入Python開發(fā)文件實現(xiàn)Python的擴展。
擴展 ctypes 類型
項目中遇到的第一個需要擴展的地方是徊哑,C/C++項目中用了C++ stl::vector袜刷。問題來了,在Python 的 ctypes 中沒相關(guān)類型的封裝呀莺丑,于是第一時間想到的是擴展 ctypes 類型著蟹。可是在實現(xiàn)的時候才發(fā)現(xiàn)這種方式是有多麻煩梢莽。
編寫 c => python 的接口文件
// vectory_py.c
extern "C" {
vector<point_t>* new_vector(){
return new vector<point_t>;
}
void delete_vector(vector<point_t>* v){
cout << "destructor called in C++ for " << v << endl;
delete v;
}
int vector_size(vector<point_t>* v){
return v->size();
}
point_t vector_get(vector<point_t>* v, int i){
return v->at(i);
}
void vector_push_back(vector<point_t>* v, point_t i){
v->push_back(i);
}
}
編譯: gcc -fPIC -shared -lpython3.6m -o vector_py.so vectory_py.c
編寫 ctypes 類型文件
from ctypes import *
class c_point_t(Structure):
_fields_ = [("x", c_int), ("y", c_int)]
class Vector(object):
lib = cdll.LoadLibrary('./vector_py_lib.so') # class level loading lib
lib.new_vector.restype = c_void_p
lib.new_vector.argtypes = []
lib.delete_vector.restype = None
lib.delete_vector.argtypes = [c_void_p]
lib.vector_size.restype = c_int
lib.vector_size.argtypes = [c_void_p]
lib.vector_get.restype = c_point_t
lib.vector_get.argtypes = [c_void_p, c_int]
lib.vector_push_back.restype = None
lib.vector_push_back.argtypes = [c_void_p, c_point_t]
lib.foo.restype = None
lib.foo.argtypes = []
def __init__(self):
self.vector = Vector.lib.new_vector() # pointer to new vector
def __del__(self): # when reference count hits 0 in Python,
Vector.lib.delete_vector(self.vector) # call C++ vector destructor
def __len__(self):
return Vector.lib.vector_size(self.vector)
def __getitem__(self, i): # access elements in vector at index
if 0 <= i < len(self):
return Vector.lib.vector_get(self.vector, c_int(i))
raise IndexError('Vector index out of range')
def __repr__(self):
return '[{}]'.format(', '.join(str(self[i]) for i in range(len(self))))
def push(self, i): # push calls vector's push_back
Vector.lib.vector_push_back(self.vector, i)
def foo(self): # foo in Python calls foo in C++
Vector.lib.foo(self.vector)
然后才是調(diào)用
from vector import *
a = Vector()
b = c_point_t(10, 20)
a.push(b)
a.foo()
for i in range(len(a)) :
print(a[i].x)
print(a[i].y)
為Python寫擴展
完成上述的操作后萧豆,我頭很大,很難想象當項目稍微修改后昏名,我們要跟隨變化的代碼量有多大涮雷!于是換了一種思路,為Python寫擴展轻局。
安裝Python開發(fā)包
yum install -y python36-devel
修改數(shù)據(jù)交互文件
#include <python3.6m/Python.h>
PyObject* foo()
{
PyObject* result = PyList_New(0);
int i = 0, j = 0;
for (j = 0; j < 2; j++) {
PyObject* sub = PyList_New(0);
for (i = 0; i < 100; ++i)
{
PyList_Append(sub, Py_BuildValue("{s:i, s:i}", "x", i, "y", 100 - i));
}
PyList_Append(result, sub);
}
return result;
}
調(diào)用
from ctypes import *
lib = cdll.LoadLibrary('./extlist.so') # class level loading lib
lib.foo.restype = py_object
b = lib.foo()
for i in range(len(b)) :
for j in range(len(b[i])) :
d = b[i][j]
print(d['x'])
很顯然洪鸭,第二種方式中,我已經(jīng)封裝了很復(fù)雜的結(jié)構(gòu)了嗽交,如果用 c++ 來表示的話卿嘲,將是:
vector<vector<point>>
遇到的問題
Python C 混編時 Segment
這個問題困擾了我有一段時間颂斜,開始一直在糾結(jié)是代碼哪錯了夫壁,后來恍然大悟,Python 和 C 的堆棧是完全不同的沃疮,而當我在交互大量數(shù)據(jù)的時候盒让,Python GC 可能會把 C 的內(nèi)存當作未使用,直接給釋放了(尤其是上述第二種方案)司蔬,這就是問題所在邑茄。(Python GC 中使用的代齡后續(xù)專門開文章來說明,歡迎關(guān)注公眾號 cn_isnap)
這里的解決方案其實有很多俊啼,內(nèi)存能撐過Python前兩代的檢查就可了肺缕,或者是純C管理。在這里我推薦一種粗暴的解決方案:
對于任何調(diào)用Python對象或Python C API的C代碼授帕,確保你首先已經(jīng)正確地獲取和釋放了GIL同木。 這可以用 PyGILState_Ensure() 和 PyGILState_Release() 來做到,如下所示:
...
/* Make sure we own the GIL */
PyGILState_STATE state = PyGILState_Ensure();
/* Use functions in the interpreter */
...
/* Restore previous GIL state and return */
PyGILState_Release(state);
...