Python 扩展技术总结(转)

时间:2023-03-09 15:31:36
Python 扩展技术总结(转)

一般来说,所有能被整合或导入到其他Python脚本中的代码,都可以称为扩展。你可以用纯Python来写扩展,也可以用C/C++之类的编译型语言来写扩展,甚至可以用java,C都可以来写 python扩展。Python的一大特点是,扩展和解释器之间的交互方式域普通的Python模块完全一样,Python的模块导入机制非常抽象,抽象到让使用模块的代码无法了解到模块的具体实现细节。

Python进行扩展的主要原因有三点:(1)添加额外的Python语言的核心部分没有提供的功能 (2)为了提升性能瓶颈的效率(3)保持专有源代码私密,把一部分代码从Python转到编译语言可以保持专有源代码私密

方法一:利用C API进行C扩展

这种方法是最基本的,包括三个步骤:1.创建应用程序代码 2.利用样板来包装代码 3.编译1.创建一个Extest.c文件 包含两个C函数

Extest.c

[cpp] view plain copy

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define BUFSIZE 10
  5. int fac(int n) {
  6. if (n < 2)
  7. return 1;
  8. return n * fac(n - 1);
  9. }
  10. //字符串反转
  11. char *reverse(char *s) {
  12. register char t;
  13. char *p = s;
  14. char *q = (s + (strlen(s) - 1));
  15. while (p < q) {
  16. t = *p;
  17. *p++ = *q;
  18. *q-- = t;
  19. }
  20. return s;
  21. }

2.用样板来包装你的代码

使用样板分为4步:

1.添加Python的头文件

2.为每一个模块的每个函数增加一个形如PyObject* Module_func()的包装函数

3.为每一个模块增加一个形如PyMethodDef ModuleMethods[]的数组

4.增加模块初始化函数 void initModule()

第二步的处理需要一些技巧,你需要为所有想Python环境访问的函数增加一个静态函数。函数的返回值类型为 PyObject*,在Python的C语言扩展接口中,大部分函数都有一个或者多个参数为PyObject指针类型,并且返回值也大都为PyObject指针. 包装函数的用处是先把Python的值传递给C,然后调用C函数把函数的计算结果转换成Python 对象,然后返回给Python

第三步每一个数组都包含一个 函数信息,包括函数Python的名字,相应的包装函数的名字以及一个METH_VARARGS常量(表示参数以元组的形式传入)

Extest.c

[cpp] view plain copy

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "Python.h"      //包含python头文件
  5. #define BUFSIZE 10
  6. int fac(int n) {
  7. if (n < 2)
  8. return 1;
  9. return n * fac(n - 1);
  10. }
  11. char *reverse(char *s) {
  12. register char t;
  13. char *p = s;
  14. char *q = (s + (strlen(s) - 1));
  15. while (p < q) {
  16. t = *p;
  17. *p++ = *q;
  18. *q-- = t;
  19. }
  20. return s;
  21. }
  22. //fac函数的包装函数
  23. static PyObject *
  24. Extest_fac(PyObject *self, PyObject *args) {
  25. int num;
  26. if (!(PyArg_ParseTuple(args, "i", &num))) {
  27. return NULL;
  28. }
  29. return (PyObject *)Py_BuildValue("i", fac(num));
  30. }
  31. //reverse函数的包装函数
  32. static PyObject *
  33. Extest_doppel(PyObject *self, PyObject *args) {
  34. char *orignal;
  35. char *reversed;
  36. PyObject * retval;
  37. if (!(PyArg_ParseTuple(args, "s", &orignal))) {
  38. return NULL;
  39. }
  40. retval = (PyObject *)Py_BuildValue("ss", orignal, reversed=reverse(strdup(orignal)));
  41. free(reversed);
  42. return retval;
  43. }
  44. //为模块创建一个函数信息的数组
  45. static PyMethodDef
  46. ExtestMethods[] = {
  47. {"fac", Extest_fac, METH_VARARGS},
  48. {"doppel", Extest_doppel, METH_VARARGS},
  49. };
  50. //增加模块初始化函数
  51. void initExtest() {
  52. Py_InitModule("Extest", ExtestMethods);
  53. }

在编译阶段需要创建一个setup.py,通过setup.py来编译和链接代码。这一步完成后就就可以直接导入这个扩展的模块了.在setup.py中需要导入distutils包。首先你要为每一个扩展创建一个Extension实例,然后编译的操作主要由setup()函数来完成,它需要两个参数:一个名字参数表示要编译哪个东西,一个列表表示要编译的对象

setup.py

[cpp] view plain copy

  1. <pre name="code" class="python">from distutils.core import setup, Extension
  2. MOD = 'Extest'
  3. setup(name=MOD, ext_modules=[
  4. Extension(MOD, sources=['Extest.c'])])

运行setup.py   :执行命令 python setup.py build

执行的结果 是在当前目录中生成一个build 目录,在此目录下有一个Extest.so文件,然后就可以在脚本中import这个模块了

方法二:利用Ctypes进行C扩展

为了扩展Python,我们可以用C/C++编写模块,但是这要求对Python的底层有足够的了解,包括Python对象模

型、常用模块、引用计数等,门槛较高,且不方便利用现有的C库。而ctypes 则另辟蹊径,通过封装

dlopen/dlsym之类的函数,并提供对C中数据结构的包装/解包,让Python能够加载动态库、导出其中的函数直

接加以利用。

一个简单的实例:

这个例子直接利用ctypes使用C标准库函数而不用编写C代码,使用C标准库函数会产生优化效果

脚本一

[python] view plain copy

  1. import timeit
  2. import random
  3. def generate(num):
  4. while num:
  5. yield random.randrange(10)
  6. num -= 1
  7. print(timeit.timeit("sum(generate(999))", setup="from __main__ import generate", number=1000))

[python] view plain copy

  1. <span style="font-size:18px;color:#FF0000;">脚本二</span>
  2. import timeit
  3. from ctypes import cdll
  4. def generate_c(num):
  5. #Load standard C library
  6. libc = cdll.LoadLibrary("libc.so.6") #Linux
  7. # libc = cdll.msvcrt #Windows
  8. while num:
  9. yield libc.rand() % 10
  10. num -= 1
  11. print(timeit.timeit("sum(generate_c(999))", setup="from __main__ import generate_c", number=1000))

第一个脚本使用 Python的随机函数,运行时间约为1.067秒 第二个脚本使用C的随机函数 运行时间约为0.423秒

我们也利用Ctypes可以自己写模块导入使用:

步骤一创建应用程序代码

[cpp] view plain copy

  1. /* functions.c */
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. /* http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#C */
  6. inline
  7. void merge(int *left, int l_len, int *right, int r_len, int *out)
  8. {
  9. int i, j, k;
  10. for (i = j = k = 0; i < l_len && j < r_len; )
  11. out[k++] = left[i] < right[j] ? left[i++] : right[j++];
  12. while (i < l_len) out[k++] = left[i++];
  13. while (j < r_len) out[k++] = right[j++];
  14. }
  15. /* inner recursion of merge sort */
  16. void recur(int *buf, int *tmp, int len)
  17. {
  18. int l = len / 2;
  19. if (len <= 1) return;
  20. /* note that buf and tmp are swapped */
  21. recur(tmp, buf, l);
  22. recur(tmp + l, buf + l, len - l);
  23. merge(tmp, l, tmp + l, len - l, buf);
  24. }
  25. /* preparation work before recursion */
  26. void merge_sort(int *buf, int len)
  27. {
  28. /* call alloc, copy and free only once */
  29. int *tmp = malloc(sizeof(int) * len);
  30. memcpy(tmp, buf, sizeof(int) * len);
  31. recur(buf, tmp, len);
  32. free(tmp);
  33. }
  34. int fibRec(int n){
  35. if(n < 2)
  36. return n;
  37. else
  38. return fibRec(n-1) + fibRec(n-2);
  39. }
  40. ~

步骤二:将它编译链接为.so文件

执行指令:gcc -Wall -fPIC -c functions.c

gcc -shared -o libfunctions.so functions.o

步骤三:在自己写的python脚本中使用这些库,下面的脚本分别用纯python的模块(输出时前面加了python),和我们导入的扩展(输出时前面加了C)模块来运行,对比其模块执行时间,

functions.py

[python] view plain copy

  1. from ctypes import *
  2. import time
  3. libfunctions = cdll.LoadLibrary("./libfunctions.so")
  4. def fibRec(n):
  5. if n<2 :
  6. return n
  7. else:
  8. return fibRec(n-1) + fibRec(n-2)
  9. start = time.time()
  10. fibRec(32)
  11. finish = time.time()
  12. print("Python: " + str(finish - start))
  13. #C Fibonacci
  14. start = time.time()
  15. x = libfunctions.fibRec(32)
  16. finish = time.time()
  17. print("C: " + str(finish - start))

functions2.py

[python] view plain copy

  1. from ctypes import *
  2. import time
  3. libfunctions = cdll.LoadLibrary("./libfunctions.so")
  4. #Python Merge Sort
  5. from random import shuffle, sample
  6. #Generate 9999 random numbers between 0 and 100000
  7. numbers = sample(range(100000), 9999)
  8. shuffle(numbers)
  9. c_numbers = (c_int * len(numbers))(*numbers)
  10. from heapq import merge
  11. def merge_sort(m):
  12. if len(m) <= 1:
  13. return m
  14. middle = len(m) // 2
  15. left = m[:middle]
  16. right = m[middle:]
  17. left = merge_sort(left)
  18. right = merge_sort(right)
  19. return list(merge(left, right))
  20. start = time.time()
  21. numbers = merge_sort(numbers)
  22. finish = time.time()
  23. print("Python: " + str(finish - start))
  24. #C Merge Sort
  25. start = time.time()
  26. libfunctions.merge_sort(byref(c_numbers), len(numbers))
  27. finish = time.time()
  28. print("C: " + str(finish - start))
  29. ~
  30. ~
  31. ~

运行结果

可以看出纯python模块的运行时间是我们写的扩展模块运行时间的十倍以上

方法三:利用Cython 进行C扩展 (参考Cython三分钟入门)

Cyhton 是一个用来快速生成 Python 扩展模块(extention module)的工具,它的语法是 python语言语法和 C 语言语法的混血。准确说 Cython 是单独的一门语言,专门用来写在 Python 里面import 用的扩展库。实际上 Cython 的语法基本上跟 Python 一致,而Cython 有专门的“编译器”;先将 Cython 代码转变成 C(自动加入了一大堆的 C-Python API),然后使用 C 编译器编译出最终的 Python可调用的模块。

要注意的一点是, Cython 是用来生成 C 扩展到而不是独立的程序的。所有的加速都是针对一个已经存在的 Python 应用的一个函数进行的。没有使用 C 或 Lisp 重写整个应用程序,也没有手写 C 扩展 。只是用一个简单的方法来整合 C 的速度和 C 数据类型到 Python函数中去

Cython 代码跟 Python 不一样,必须要编译。 编译经过两个阶段:(1) Cython 编译.pyx 文件为.c 文件 (2) C 编译器会把.c 文件编译成.so 文件.生成.so 文件后表示重写函数成功,可以在 python 代码中直接调用这个模块

Python代码

  1. #p1.py
  2. import math
  3. def great_circle(lon1,lat1,lon2,lat2):
  4. radius = 3956 #miles
  5. x = math.pi/180.0
  6. a = (90.0-lat1)*(x)
  7. b = (90.0-lat2)*(x)
  8. theta = (lon2-lon1)*(x)
  9. c = math.acos((math.cos(a)*math.cos(b)) +
  10. (math.sin(a)*math.sin(b)*math.cos(theta)))
  11. return radius*c

Python代码

  1. #p1_test.py
  2. import timeit
  3. lon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826
  4. num = 500000
  5. t = timeit.Timer("p1.great_circle(%f,%f,%f,%f)" % (lon1,lat1,lon2,lat2),
  6. "import p1")
  7. print "Pure python function", t.timeit(num), "sec"

Python代码

  1. # 测试结果
  2. Pure python function 2.25580382347 sec

Cython: 使用Python的math模块

Python代码

  1. #c1.pyx
  2. import math
  3. def great_circle(float lon1,float lat1,float lon2,float lat2):
  4. cdef float radius = 3956.0
  5. cdef float pi = 3.14159265
  6. cdef float x = pi/180.0
  7. cdef float a,b,theta,c
  8. a = (90.0-lat1)*(x)
  9. b = (90.0-lat2)*(x)
  10. theta = (lon2-lon1)*(x)
  11. c = math.acos((math.cos(a)*math.cos(b)) + (math.sin(a)*math.sin(b)*math.cos(theta)))
  12. return radius*c

Python代码

  1. # setup.py
  2. from distutils.core import setup
  3. from distutils.extension import Extension
  4. from Cython.Distutils import build_ext
  5. ext_modules=[
  6. Extension("c1",
  7. ["c1.pyx"])
  8. ]
  9. setup(
  10. name = "Demos",
  11. cmdclass = {"build_ext": build_ext},
  12. ext_modules = ext_modules
  13. )

python setup.py build_ext --inplace

Python代码

  1. #c1_test.py
  2. import timeit
  3. lon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826
  4. num = 500000
  5. t = timeit.Timer("c1.great_circle(%f,%f,%f,%f)" % (lon1,lat1,lon2,lat2),
  6. "import c1")
  7. print "Pure python function", t.timeit(num), "sec"

Python代码

  1. #执行结果:
  2. Pure python function 1.87078690529 sec

Cython:使用C的math库

Python代码

  1. #c2.pyx
  2. cdef extern from "math.h":
  3. float cosf(float theta)
  4. float sinf(float theta)
  5. float acosf(float theta)
  6. def great_circle(float lon1,float lat1,float lon2,float lat2):
  7. cdef float radius = 3956.0
  8. cdef float pi = 3.14159265
  9. cdef float x = pi/180.0
  10. cdef float a,b,theta,c
  11. a = (90.0-lat1)*(x)
  12. b = (90.0-lat2)*(x)
  13. theta = (lon2-lon1)*(x)
  14. c = acosf((cosf(a)*cosf(b)) + (sinf(a)*sinf(b)*cosf(theta)))
  15. return radius*c

Python代码

  1. #setup.py
  2. from distutils.core import setup
  3. from distutils.extension import Extension
  4. from Cython.Distutils import build_ext
  5. ext_modules=[
  6. Extension("c2",
  7. ["c2.pyx"],
  8. libraries=["m"]) # Unix-like specific
  9. ]
  10. setup(
  11. name = "Demos",
  12. cmdclass = {"build_ext": build_ext},
  13. ext_modules = ext_modules
  14. )

python setup.py build_ext --inplace

Python代码

  1. # c2_test.py  
  2. import timeit  
  3. lon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826  
  4. num = 500000  
  5. t = timeit.Timer("c2.great_circle(%f,%f,%f,%f)" % (lon1,lat1,lon2,lat2),  
  6.                        "import c2")  
  7. print "Pure python function", t.timeit(num), "sec"  

Python代码

  1. #执行结果  
  2. Pure python function 0.34069108963 sec   

Cython:使用C函数

Python代码

  1. #c3.pyx
  2. cdef extern from "math.h":
  3. float cosf(float theta)
  4. float sinf(float theta)
  5. float acosf(float theta)
  6. cdef float _great_circle(float lon1,float lat1,float lon2,float lat2):
  7. cdef float radius = 3956.0
  8. cdef float pi = 3.14159265
  9. cdef float x = pi/180.0
  10. cdef float a,b,theta,c
  11. a = (90.0-lat1)*(x)
  12. b = (90.0-lat2)*(x)
  13. theta = (lon2-lon1)*(x)
  14. c = acosf((cosf(a)*cosf(b)) + (sinf(a)*sinf(b)*cosf(theta)))
  15. return radius*c
  16. def great_circle(float lon1,float lat1,float lon2,float lat2,int num):
  17. cdef int i
  18. cdef float x
  19. for i from 0 <= i < num:
  20. x = _great_circle(lon1,lat1,lon2,lat2)
  21. return x

C-sharp代码

  1. #setup.py
  2. from distutils.core import setup
  3. from distutils.extension import Extension
  4. from Cython.Distutils import build_ext
  5. ext_modules=[
  6. Extension("c3",
  7. ["c3.pyx"],
  8. libraries=["m"]) # Unix-like specific
  9. ]
  10. setup(
  11. name = "Demos",
  12. cmdclass = {"build_ext": build_ext},
  13. ext_modules = ext_modules
  14. )

python setup.py build_ext --inplace

Python代码

  1. #c3_test.py
  2. import timeit
  3. lon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826
  4. num = 500000
  5. t = timeit.Timer("c2.great_circle(%f,%f,%f,%f)" % (lon1,lat1,lon2,lat2),
  6. "import c2")
  7. print "Pure python function", t.timeit(num), "sec"

Python代码

  1. #测试结果
  2. Pure python function 0.340164899826 sec

测试结论

Python代码

    • # python代码
    • Pure python function 2.25580382347 sec
    • # Cython,使用Python的math模块
    • Pure python function 1.87078690529 sec
    • # Cython,使用C的math库
    • Pure python function 0.34069108963 sec
    • # Cython,使用纯粹的C函数
    • Pure python function 0.340164899826 sec

注意事项:

通过cython扩展python 模块时出现“ImportError: No module named Cython.Build“的解决方法如下

pip install Cython

pip install fasttext

这个pip必须与当前python版本相一致

参考:  https://blog.****.net/u010786109/article/details/41825147#